简体   繁体   中英

Create separate Python dictionary from nested Json

I have a data in below format, which is nested Json. I am looking to create a dictionary based on values of nested json.

Json Data in below format

data = {
    "0": {
        "0": "Accomplishments:",
        "1": "Testing"
    },
    "1": {
        "0": "Priorities:",
        "1": "Priorities value"
    },
    "2": {
        "0": "agenda:",
        "1": "3 months"
    }
}

Expected outcome is as below

{
    "Accomplishments:" : "Testing",
    "Priorities:" : "Testing",
    "agenda:" : "3 months"
}
result = {}
for subdict in data.values():
    result[subdict["0"]] = subdict["1"]
print(result)

Or with dict comprehension:

result = {subdict["0"]: subdict["1"] for subdict in data.values()}
print(result)

Result: {'Accomplishments:': 'Testing', 'Priorities:': 'Priorities value', 'agenda:': '3 months'}

To dump as json string, do result_as_json = json.dumps(result)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM