I have this structured df (json) which includes another json and I want to combine them to access all the values:
{
"index": "exp-000005",
"type": "_doc",
"score": 9.502488,
"source": {
"verb": "REPLIED",
"timestamp": "2022-01-20T08:14:00+00:00",
"in_context": {
"screen_width": "3440",
"screen_height": "1440",
"build_version": "7235",
"question": "Hallo",
"request_time": "403",
"status": "success"
}
}
}
The result I want is the following:
{
"index": "exp-000005",
"type": "_doc",
"score": 9.502488,
"verb": "REPLIED",
"timestamp": "2022-01-20T08:14:00+00:00",
"screen_width": "3440",
"screen_height": "1440",
"build_version": "7235",
"question": "Hallo",
"request_time": "403",
"status": "success"
}
How can I combine this structured json to one unstructured json? In R I used tbl_json. Maybe there is something similar for python?
>Solution :
Looks like you want to flatten the dict and only pick the values of the keys which are themselves dictionary.
Here is a recursive approach:
from pprint import pprint
d = {
"index": "exp-000005",
"type": "_doc",
"score": 9.502488,
"source": {
"verb": "REPLIED",
"timestamp": "2022-01-20T08:14:00+00:00",
"in_context": {
"screen_width": "3440",
"screen_height": "1440",
"build_version": "7235",
"question": "Hallo",
"request_time": "403",
"status": "success",
},
},
}
def flatten(d: dict, result_d={}):
for k, v in d.items():
if isinstance(v, dict):
flatten(v, result_d)
else:
result_d[k] = v
return result_d
new_d = flatten(d)
pprint(new_d, sort_dicts=False)
output:
{'index': 'exp-000005',
'type': '_doc',
'score': 9.502488,
'verb': 'REPLIED',
'timestamp': '2022-01-20T08:14:00+00:00',
'screen_width': '3440',
'screen_height': '1440',
'build_version': '7235',
'question': 'Hallo',
'request_time': '403',
'status': 'success'}