Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

python – Combine structured JSON with remaining JSON

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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'}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading