1import json
2
3with open('path_to_file/person.json') as f:
4 data = json.load(f)
5
6print(data)
1# Basic syntax:
2import ast
3# Create function to import JSON-formatted data:
4def import_json(filename):
5 for line in open(filename):
6 yield ast.literal_eval(line)
7# Where ast.literal_eval allows you to safely evaluate the json data.
8# See the following link for more on this:
9# https://stackoverflow.com/questions/15197673/using-pythons-eval-vs-ast-literal-eval
10
11# Import json data
12data = list(import_json("/path/to/filename.json"))
13
14# (Optional) convert json data to pandas dataframe:
15dataframe = pd.DataFrame.from_dict(data)
16# Where keys become column names
1>>> import json
2>>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')
3['foo', {'bar': ['baz', None, 1.0, 2]}]
4>>> json.loads('"\\"foo\\bar"')
5'"foo\x08ar'
6>>> from io import StringIO
7>>> io = StringIO('["streaming API"]')
8>>> json.load(io)
9['streaming API']
10