Parse JSON data in Python [May 29]

:raising_hand_man: TastierSub asked

As someone relatively new to Python, how exactly would I parse the json response from one of my NocoDB databases?

{"list":[{"Id":1,"Title":"A Game of Thrones","Word Count":"292727","Custom Sort":"1","Publish Year":1996,"Publish Month":"August","Author":[{"Id":1,"Name":"George R. R. Martin"}],"In-Universe Series":[{"Id":1,"In-Universe Series":"A Song of Ice and Fire"}],"Series":["A Song of Ice and Fire"],"Universe":["A Song of Ice and Fire"]},...

It’s essentially a list of books and relevant information. I’m hoping to extract certain values from each record (Title, Author, and Word Count) to plug into a pandas dataframe.

:raising_hand_man: FrancyMak replied

Hi! So.

```py

import json

person = ‘{“name”: “Bob”, “languages”: [“English”, “French”]}’

person_dict = json.loads(person)

# Output: {‘name’: ‘Bob’, ‘languages’: [‘English’, ‘French’]}

print( person_dict)

# Output: [‘English’, ‘French’]

print(person_dict[‘languages’])

```

:raising_hand_man: FrancyMak replied

If you want to use a file:

```py

import json

with open(‘path_to_file/person.json’, ‘r’) as f:

data = json.load(f)

# Output: {‘name’: ‘Bob’, ‘languages’: [‘English’, ‘French’]}

print(data)

```

:raising_hand_man: FrancyMak replied

Don’t forget the “r” after the file path, it means “read”

Autogenerated from discord


Join NocoDB’s community

DiscordTwitterRedditDocs