python json parser example
Example 1: how to parse json in python
import json
x = '{ "name":"John", "age":30, "city":"New York"}'
y = json.loads(x)
print(y["age"])
Example 2: load json
import json
with open('data.txt') as json_file:
data = json.load(json_file)
Example 3: python to json
x = {
"name": "John",
"age": 30,
"city": "New York"
}
y = json.dumps(x)
Example 4: python import json data
import ast
def import_json(filename):
for line in open(filename):
yield ast.literal_eval(line)
data = list(import_json("/path/to/filename.json"))
dataframe = pd.DataFrame.from_dict(data)