Contentful logo

Contentful Community

How can I get the items from GetEntries as JObjects so I can access the fields and metadata as Json?

I am calling GetEntries and the Fields in the Items is always null. I would like the full Json as a JObject or Json string containing both the SystemProperties and Fields. I want to handle all ContentTypes generically since I many not know the content model.

var builder = new QueryBuilder<Entry>().Include(2).OrderBy(“sys.updatedAt”).Skip(skip);
var entryCollection = await _contentfulClient.GetEntries(builder);
foreach (var item in entryCollection) {
JObject entryJObject = JObject.FromObject(item);
}

Hi Lisa

You can just set the generic parameter to a JObject QueryBuilder<JObject> or dynamic QueryBuilder<dynamic> and you should be able to iterate through the JObject as you would expect.

Hope this helps.

Best
Robert

Thanks. I’m confused that the JObject representing an entry from this call via the SDK looks “flattened” in comparison to the version one sees when using curl. I’m accustomed to seeing two properties, “sys” and “fields”. Is there a way to get the curl version back as the JObject?

The reason is I’m serializing the results to be shared with another component in my system that is expecting the entry json to look like schema returned by curl
{
“sys”: {…},
“fields”:{…}
}

Hi Lisa!

Yes, that’s part of what the SDK does to make serialization more logical when you use strongly typed objects.

Currently, there’s no option to get the original structure back, unfortunately.

What you could do is to modify the JObject yourself and simply move all the properties (except the sys prop) into a property called fields and you would be back to the original structure. However, there are also other things that the SDK does, such as adding $ref and $id properties to resolve referenced content, which would also potentially interfere with your expected schema.

If you only wish to proxy the call to the Contentful API and return the response I would suggest using the HttpClient directly and issue a get request. Perhaps something similar to what I did in my old article here: https://robertlinde.se/contentful/cms/asp.net%20core/aurelia/2016/05/06/building-with-asp.net-core,-aurelia-and-contentful.html

Best
Robert