0

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?

martineau
  • 119,623
  • 25
  • 170
  • 301
Rushabh Mehta
  • 1,529
  • 1
  • 13
  • 29
  • 1
    Does this answer your question? [Reading rather large json files in Python](https://stackoverflow.com/questions/10382253/reading-rather-large-json-files-in-python) – tsamridh86 Sep 04 '21 at 16:05
  • Neither of these answer my question since they don't allow me to select which parts of the json to unwrap. – Rushabh Mehta Sep 04 '21 at 16:24
  • 1
    Don: Doesn't matter, it's the same problem. JSON doesn't lend itself to being read incrementally or piecemeal (because it's a free-form text file format). – martineau Sep 04 '21 at 16:28

0 Answers0