1import json
2
3x = '{ "name":"John", "age":30, "city":"New York"}'
4y = json.loads(x)
5
6print(y["age"])
1import json
2
3json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
4'["foo", {"bar": ["baz", null, 1.0, 2]}]'
5
6print(json.dumps({"c": 0, "b": 0, "a": 0}, sort_keys=True))
7{"a": 0, "b": 0, "c": 0}
8
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