Skip to content

Converting Entry to JSON does not include field values for multi-nested resourcesΒ #79

@kfleitman06

Description

@kfleitman06

I am currently using the Contentful client in Python within one of our APIs to get entries matching a query and return the entries as a response using ujson.

When we get an entry using client.entries(), we are able to see all linked resources are hydrated properly in each entry's _fields property, however Python does not include properties for objects that are prefixed with _ when serializing to json using ujson or json.

https://github.com/contentful/contentful.py/blob/master/contentful/resource.py#L113

This means that when we return the response of a list of entries with multiple nested resources from our API, we are unable to use the nested entries due to not having the hydrated fields resources, despite them having originally been hydrated

Roughly, for example:

When looking at the only entry in a list in the response from client.entries(), where the content is multiple levels of nested references

a_entry.fields() = {
  title: "Entry A",
  content: [
    B_Entry: {
      sys: {...},
      _fields: {
         title: "Entry B",
         content: [
           D_Entry: {
               sys: {...},
              _fields: { 
                  title: "Entry D",
                   ...
              }
           }
        ]
      }
    },
    C_Entry: {
      sys: {...},
      _fields: {
        title: "Entry C",
        ....  
        }
     },
  ]
}

serializes to json:

 {
  title: "Entry A",
  content: [
  {
   "sys": {...},
  },
  {
   "sys": {...},
  }
]

}

Where sys only holds references to the link IDs, and all actual fields within _fields are lost that aren't in the top-level entry

Is there something that I am missing here to be able to easily have the field values included as JSON?

I understand that we can set the value of each entry to a new key by doing [entry].fields(), but when we have many levels of nested resources included, it feels complex to have to go back through each level to set a key like field to be the value of _field after it was already built

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions