NOTE: Your JSON response from MongoDB is not actually valid. JSON requires double-quotes ("), not single-quotes (').

I'm not sure why your response has single-quotes instead of double-quotes but from the looks of it you can replace them and then just use the built-in json module:

from __future__ import print_function
import json

response = """{
    'ok': 1.0, 
    'result': [
        {
            'total': 142250.0, 
            '_id': 'BC'
        }, 
        {
            'total': 210.88999999999996,
             '_id': 'USD'
        }, 

        {
            'total': 1065600.0, 
            '_id': 'TK'
        }
        ]
}"""

# JSON requires double-quotes, not single-quotes.
response = response.replace("'", '"')
response = json.loads(response)
for doc in response['result']:
    print(doc['_id'], doc['total'])
Answer from ohmu on Stack Overflow
🌐
Pybites
pybit.es › articles › case-study-how-to-parse-nested-json
Case Study: How To Parse Nested JSON - Pybites
June 3, 2022 - A common strategy is to flatten the original JSON by doing something very similar like we did here: pull out all nested objects by concatenating all keys and keeping the final inner value.
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-parse-nested-json-in-python
How to Parse Nested JSON in Python - GeeksforGeeks
July 23, 2025 - In this article, we will discuss multiple ways to parse nested JSON in Python using built-in modules and libraries like json, recursion techniques and even pandas. Nested JSON refers to a JSON object that contains another JSON object (or an array of objects) inside it.
🌐
Reddit
reddit.com › r/learnpython › better way to parse insanely complex nested json data
r/learnpython on Reddit: Better way to parse insanely complex nested json data
July 17, 2024 -

Hi all,

Long time programmer here, but new to Python. This will be a long and, I think, complicated issue, so appreciate anyone who reads through it all and has any suggestions. I've looked up different ways to pull this data and don't seem to be making any progress. I'm sure there's a much better way.

I'm writing a program that will connect to our library to pull a list of everything we have checked out and I want to output a sorted list by due date and whether it has holds or not. I've got the code working to log in and pull a json data structure, but I cannot get it to export the data in the correct order. The json data is(to me) hideously complex with some data(due date) in one section and other data in another section. I'm able to pull the fields I want, but keeping them together is proving challenging.

For example, the title and subtitle are in the 'bibs/briefinfo' section with a key value of 'title' or 'subtitle'. Due Date is also in the 'checkouts' section with a key value of 'dueDate'. When I loop through them, though, the Titles are in one order, the due dates are in another order and the subtitles another.

I used BeautifulSoup because it's a webpage with json in it, so used BS to read the webpage.

I'm wanting to pull the following fields for each book so I can display the info for each book:

title, subtitle, contentType from briefinfo section

duedate from checkouts section

heldcopies and availablecopies from the availability section

Here's the pertinent section of my code:

soup = BeautifulSoup(index_page.text, 'html.parser')
            all_scripts = soup.find_all('script', {"type":"application/json"})

            for script in all_scripts:
                jsondata = json.loads(script.text)
                print(jsondata)
                
                output = []
                for i in item_generator(jsondata, "bibTitle"):
                    ans = {i}
                    print(i)
                    output.append(ans)

                for i in item_generator(jsondata, "dueDate"):
                    ans = {i}
                    output.append(ans)

                print("Subtitle----------------------")
                for i in item_generator(jsondata, "subtitle"):
                    ans = {i}
                    print(i)
                    output.append(ans)

print(output)

Here's the json output from my print statement so I can see what I'm working with. I tried to format it so it's easier to read. I removed a lot of other elements to keep the size down. Hopefully I didn't break any of the brackets.

{

'app':

{

'coreCssFingerprint': '123123123',

'coreAssets':

{

'cdnHost': 'https://xyz.com',

'cssPath': '/dynamic_stylesheet',

'defaultStylesheet': 'xyz.css'

},

},

'entities':

{

'listItems': {},

'cards': {},

'accounts':

{

'88888888':

  {
  'barcode': '999999999',
  'expiryDate': None, 
  'id': 88888888, 
  }

},

'shelves':

  {
  '88888888': 
  	{
  	'1111222222': 

{

'id': 1111222222,

'metadataId': 'S00A1122334',

'shelf': 'for_later',

'privateItem': True,

'dateAdded': '2023-12-30',

},

  	}
  }, 

'users':

  {
  '88888888': 
  	{ 
  	'accounts': \[88888888\], 
  	'status': 'A', 
  	'showGroupingDebug': False, 
  	'avatarUrl': '', 
  	'id': 88888888, 
  	}
  }, 
  'eventPrograms': {}, 
  'checkouts': 
  	{
  	'112233445566778899': 

{

'checkoutId': '112233445566778899',

'materialType': 'PHYSICAL',

'dueDate': '2024-08-26',

'metadataId': 'S99Z000000',

'bibTitle': "The Lord of the Rings"

},

  	'998877665544332211': 

{

'checkoutId': 998877665544332211',

'materialType': 'PHYSICAL',

'dueDate': '2024-08-26',

'metadataId': 'S88Y00000',

'bibTitle': 'The Lord of the Rings'

},

  	}, 
  'eventSeries': {}, 
  'catalogBibs': {},
  'bibs': 
  	{
  	'S88Y00000': 

{

'id': 'S88Y00000',

'briefInfo':

{

'superFormats': ['BOOKS', 'MODERN_FORMATS'],

'genreForm': [],

'callNumber': '123.456',

'authors': ['Tolkien, J.R.R.'],

'metadataId': 'S88Y00000',

'jacket':

{

'type': 'hardcover',

'local_url': None

},

'contentType': 'FICTION',

'format': 'BK',

'subtitle': 'The Two Towers',

'title': 'The Lord of the Rings',

'id': 'S88Y00000',

},

'availability':

{

'heldCopies': 0,

'singleBranch': False,

'metadataId': 'S88Y00000',

'statusType': 'AVAILABLE',

'totalCopies': 3,

'availableCopies': 2

}

},

'S77X12345':

{

'id': 'S77X12345',

'briefInfo':

{

'superFormats': ['BOOKS', 'MODERN_FORMATS'],

'genreForm': [],

'callNumber': '123.457',

'authors': ['Tolkien, J.R.R.'],

'metadataId': 'S77X12345',

'jacket':

{

'type': 'hardcover',

'local_url': None

},

'contentType': 'FICTION',

'format': 'BK',

'subtitle': 'The Fellowship of the Ring',

'title': 'The Lord of the Rings',

'id': 'S77X12345',

},

'availability':

{

'heldCopies': 0,

'singleBranch': False,

'metadataId': 'S77X12345',

'statusType': 'AVAILABLE',

'totalCopies': 2,

'availableCopies': 1

}

}

Anyone know of a better way to parse this data? Thanks!

Top answer
1 of 4
5
Two suggestions: First, when you want to print a json thing, you can do print(json.dumps(thing, indent=2)). This will apply indentation and newlines to make it clearer what the nested structure is. Second, the input data is what it is - but internal to your code you don't have to keep it that way. My suggestion would be to make a dataclass with the fields you care about, and write a classmethod for that that class to extract what you care about from the json. Then in your code, use instances of your class. I'm on my phone right now, but I'll edit with a small example of what I mean in a little bit. gross_nested_json = { 'books': [ { 'name': 'whatever', 'details1': { 'due_date': 'whenever', }, 'details2': { 'whatever_else': 'thing', } } ] } import dataclasses import typing as ty @dataclasses.dataclass class BookInfo: # If you're not familiar with these, google python dataclass, they're nice name: str due_date: str whatever: str @classmethod def from_gross_json(cls, gross_json: dict) -> ty.Self: return cls( name=gross_json['name'], due_date=gross_json['details1']['due_date'] whatever=gross_json['details2']['whatever_else'] ) books = [BookInfo.from_gross_json(gross_json) for gross_json in gross_nested_json['books'] You'll have to adjust for the pecularities of your particular input data, but if you make the data less gross for within your code consumption, it'll make the rest of your program nicer to write.
2 of 4
2
That's a rather awkward structure alright. Are the records to be linked by metadataId? [In]: for checkout in data['entities']['checkouts'].values(): print(checkout['bibTitle'], checkout['dueDate'], checkout['metadataId']) for bib in data['entities']['bibs'].values(): print(bib['briefInfo']['title'], bib['briefInfo']['subtitle'], bib['briefInfo']['metadataId']) print(bib['availability']['heldCopies'], bib['availability']['availableCopies']) [Out]: # The Lord of the Rings 2024-08-26 S99Z000000 # The Lord of the Rings 2024-08-26 S88Y00000 # The Lord of the Rings The Two Towers S88Y00000 # 0 2 # The Lord of the Rings The Fellowship of the Ring S77X12345 # 0 1
🌐
Zyte
zyte.com › home › blog › json parsing with python [practical guide]
JSON Parsing with Python [Practical Guide]
July 6, 2023 - Learn how to parse flat and nested JSON data with Python. This guide covers libraries, methods, and advanced json parsers like JMESPath and ChompJS.
🌐
LabEx
labex.io › tutorials › python-what-are-best-practices-for-extracting-values-from-nested-python-json-objects-395100
What are best practices for extracting values from nested Python JSON objects | LabEx
Type of data: <class 'dict'> Keys at the top level: ['person'] Keys in the person object: ['name', 'age', 'contact', 'address', 'hobbies', 'employment'] This confirms that our JSON file has been loaded correctly as a Python dictionary. In the next step, we will explore different methods to extract values from this nested structure.
🌐
Bcmullins
bcmullins.github.io › parsing-json-python
Parsing Nested JSON Records in Python - Brett Mullins – Researcher - Data Scientist
This is how both ‘Alice’ and ‘Bob’ are returned; since the value of employees is a list, the nesting is split on both of its elements and each of the values for name are appended to the output list. If obj is a single dictionary/JSON record, then this function returns a list containing the desired information, and if obj is a list of dictionaries/JSON records, then this function returns a list of lists containing the desired information.
🌐
Medium
ankushkunwar7777.medium.com › get-data-from-large-nested-json-file-cf1146aa8c9e
Working With Large Nested JSON Data | by Ankush kunwar | Medium
January 8, 2023 - You can access the data in the ... from a nested JSON object using recursion, you can use a function that iterates through the object and extracts the desired values....
Find elsewhere
🌐
YouTube
youtube.com › watch
PARSING EXTREMELY NESTED JSON: USING PYTHON | RECURSION - YouTube
This is a video for those wanting to stop nightmares from 𝐧𝐞𝐬𝐭𝐞𝐝 𝐉𝐒𝐎𝐍 files. This is about as nested as you get in this video. We will use recursio...
Published   July 16, 2020
🌐
Quora
quora.com › What-is-the-best-way-to-parse-a-nested-JSON-structure-using-Python
What is the best way to parse a nested JSON structure using Python? - Quora
Use streaming parsers for large data and optimized JSON engines for speed. ... Without knowing the nature of the data or the type of structure, you should simply use json.loads to load the document to a Python object and then examine its contents in a debugger.
🌐
Hackers and Slackers
hackersandslackers.com › extract-data-from-complex-json-python
Extract Nested Data From Complex JSON
December 22, 2022 - It's a great full-featured API, but as you might imagine the resulting JSON for calculating commute time between where you stand and every location in the conceivable universe makes an awfully complex JSON structure. We're going to use the Google Maps API to get the distance between two locations, as well as the estimated duration to complete the trip. Below we see how such a request would be made via Python's requests library.
🌐
Quora
quora.com › How-do-I-extract-nested-JSON-data-in-python
How to extract nested JSON data in python - Quora
Answer (1 of 5): [code]for item in array_name: print item.get("arrayElementName").get("NestedarrayElementName") [/code]
🌐
LabEx
labex.io › tutorials › python-how-to-access-nested-keys-in-a-python-json-object-395034
How to access nested keys in a Python JSON object | LabEx
JSON objects often contain nested structures. In Python, we can access nested data using multiple square brackets or by chaining dictionary keys.
🌐
Plain English
plainenglish.io › blog › data-extraction-parse-a-3-nested-json-object-23cb978b66ad
Data Extraction: Parse a 3-Nested JSON Object and Convert it to a pandas dataframe
June 6, 2021 - The response variable in the below code, stores the returned values, and the JSON output is parsed using the json( ) method. ... The request.get method has the following parameter values: requests.get(url, params={key: value}, args). Detailed documentation can be looked at here: Python Requests get Method *❮ Requests Module Make a request to a web page, and return the status code: import requests x =…*www.w3schools.com · A JSON object is a collection of key and value pairs.
🌐
Nadreck
nadreck.me › 2018 › 01 › extracting-a-nested-json-value-in-python
Extracting a Nested JSON Value in Python – nadreck.me
July 31, 2021 - import json from jsonpath_rw import parse with open('/path/to/myfile.json') as f: json_data = json.load(f) get_my_value = parse('$.objectArray[1].nestedDict.anotherArray[0].valueIWant').find(json_data) print get_my_value
🌐
Reddit
reddit.com › r/learnpython › parsing nested json
r/learnpython on Reddit: Parsing nested JSON
February 9, 2022 -

Hello,

Looking for help/advice on parsing nested JSON.

Background:

I receive a TXT file everyday containing JSON structured data. I need to parse through all of the JSON structure it into an Excel format with nice columns and rows where every column is the key and every row is its value.

Current data structure:

{
"key": "value",
"key": "value",
"key":  "{\"key\":value,\"key\":value,\"key\":[{\"key\":\"value\" etc etc
}

Current process:

file_location = r'<location of txt file containing json>'
with open(file_location) as json_file:
    data = json,load(json_file)

This allows me access to the high level key-value pairs, but does not allow me access to the nested data.

I've tried a dozen different ways to access nested data and thus far have not been successful. The nested JSON is returning as a string like:

nested_json = data['<key>']

nested_data = json.loads(nested_json)

I tried the code above thinking I must have to manually flatten each level of nested data. But with multiple levels of nested data in various places throughout the entire file, this would take me forever.

An issue I keep bumping into is that the nested portion of the data returns as STRING type rather than a dictionary, preventing me from referencing an index like this:

print(data['<key>'][1])

If I try that, it simply returns the first character in the key itself.

Additional attempts:

df = pd.DataFrame.from_dict(pd.json_normalize(data), orient='columns') df = pd.json_normalize('<key'>)

I have around 40 different scripts I've tried, read various blogs, websites, stackoverflow, etc. And I still don't understand what I'm doing wrong.

Can anyone help please?

*I would post the data itself but it's restricted data I can't post online. If you need to see more code attempts let me know and I can copy more here.

tl;dr - need to parse all JSON data included all nested key-value pairs and get them in an Excel like format (Pandas/csv/txt/etc. is perfectly fine).

Thank you!

🌐
Reddit
reddit.com › r/learnpython › parsing insane nested json data from an api
r/learnpython on Reddit: Parsing insane nested JSON data from an API
November 10, 2016 -

So maybe I am burnt out on this but I looked around on stack overflow and couldn't really find an answer that made sense to me.

Im trying to work with this weather API that gives the forecast and it return this insane JSON block that I have no idea how to parse. I don't even know where dictionaries begin and end in it. So if I wanted to spit out the current temp and indicate that it is Fahrenheit from the below link how would I do that?

https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22milwaukee%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys

Hopefully someone can help, thanks

EDIT:

Thanks for the responses. I'm a bit further along than what I posted and should have indicated that, sorry.

Im at:

  • import requests

  • import json

  • url = 'crazy-string'

  • r= requests.get(url)

  • r_json = r.json()

I'm working on how to parse through r_json and get values for specific keys

EDIT 2:

corvus_cornix had the answer I was looking for :)

🌐
Python Forum
python-forum.io › thread-38090.html
Parse Nested JSON String in Python
Hello All, I am trying to parse complex JSON String from AWS Pricing JSON file and convert it into CSV file based on some conditions. I was able to achieve the parsing code implementation using for lo