json
JavaScript Object Notation
Last updated
Was this helpful?
JavaScript Object Notation
Last updated
Was this helpful?
The JSON format was inspired by the object and array format used in the JavaScript language. But since Python was invented before JavaScript, Pythonโs syntax for dictionaries and lists influenced the syntax of JSON. So the format of JSON is nearly identical to a combination of Python lists and dictionaries. Here is a JSON encoding that is roughly equivalent to the simple XML format:
Source: Python
a JSON object looks like a dictionary. It can have a dictionary as a value, and this can lead to a dictionary in a dictionary tree. It is derived from Javascript in which an object is described as a dictionary.
We can load JSON data into a python JSON object with the methodjson.load()
. This is especially handy if we want only certain keys of the JSON file. In the example above I want to make a DataFrame of the hits with a record for each ID. I load the entire JSON file and subtract the hits tree with the method pd.DataFrame.from_dict()
I can investigate the structure of the JSON file by retrieving the keys. In the example below the key's max_score
, took
, total
and hits
are returned
It seems that the hits key contain interesting records we want to investigate further. These I will parse in a pandas DataFrame
โ
_id
_score
entrezgene
name
symbol
taxid
0
8660
5.904780
8660
insulin receptor substrate 2
IRS2
9606
1
3667
5.812647
3667
insulin receptor substrate 1
IRS1
9606
9
3651
5.288981
3651
pancreatic and duodenal homeobox 1
PDX1
9606
We can also define a JSON object and write it to a JSON file. We use the method json.dump()
to do such
โ
Mind you if you want to write a DataFrame to a JSON object you need to think about the orientation
we can use to validate the output
โโ