Read JSON in python

Ulf Hamster 1 min.
python json data collection

How to read flat JSON files (JavaScript Object Notation)?

The demo file

!cat data/demo1.json
[
    {"name": "John Doe", "id": 123, "value": 99.67},
    {"name": "Manuel Neuer", "id": 6000, "value": 1.0}
]

JSON to dict

  1. Open a JSON file, and read as str.
  2. Convert the JSON string to dict.
import json
dat = json.loads(open("data/demo1.json").read())
print(dat)
[{'name': 'John Doe', 'id': 123, 'value': 99.67}, {'name': 'Manuel Neuer', 'id': 6000, 'value': 1.0}]

JSON to pandas.DataFrame

  1. Open a JSON file and read as str.
  2. Convert JSON string to dict.
  3. Create pandas DataFrame from dict.
import json
import pandas as pd
dj = open("data/demo1.json").read()
dd = json.loads(dj)
df = pd.DataFrame.from_dict(dd) 
df
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}