In Python, when we want to load in JSON data, we use json.load
(or json.loads
).
The issue with these functions is that they fully parse the JSON: i.e., if I have the JSON:
{"fruits":
{"apple": ["golden delicious","pink lady","gala"],
"mango": ["kent", "alfonso"]}
"vegetables":
["onions", "carrots", "celery"]
}
json.load
will return a nested dictionary/list structure.
However, I have a rather large JSON, that I don't want to fully unwrap. I just want to unwrap the first layer, i.e., in the JSON above, I want to return:
{"fruits" : '''{"apple": ["golden delicious","pink lady","gala"],"mango": ["kent", "alfonso"]}''',
"vegetables" : '''["onions", "carrots", "celery"]'''
}
Or, I'd like to just extract the JSON that is the value to the key "fruits"
. How do I unwrap JSON piece by piece in Python 3?