I have the following JSON file:
[
{'docType': 'custom',
'fields':
{
'general_info': None,
'power': 20,
'safety':
{
'boundingBox': [2.375,9.9,4.98,9.9,4.98,10.245,2.375,10.245],
'confidence': 0.69,
'page': 22,
'text': 'bla-bla-bla',
'type': 'string',
'valueString': 'bla-bla-bla'
},
'replacement':
{
'boundingBox': [2.505,2.51,2.54,2.51,2.54,3.425,2.505,3.425],
'confidence': 0.262,
'page': 7,
'text': 'bla-bla-bla',
'type': 'string',
'valueString': 'bla-bla-bla'
},
'document_id': 'x123'
}
}
]
I want to go through all field values and extract text from nested fields. The expected results is as the follows:
{
'labels':
{
'general_info': None,
'power': 20,
'safety': 'bla-bla-bla',
'replacement': 'bla-bla-bla',
'document_id': 'x123'
}
}
How can I flatted my JSON file and get an expected result?
This is what I have tried so far:
import json
json_object = json.load(raw_json)
fields = {}
for field in json_object:
for attribute, value in field.items():
fields[attribute] = value
fields_json = json.dumps(fields, indent = 4)
However, I don't know how to recursively enter into nested fields