Read Json files in python
Json files with no spaces or newlines
-
Inputting json file into python with
json.loads(_)
, -
Store a dictionary of a dictionary
1
2
3
# Store the json into a dictionary for all the entries
with open('sample.json') as entry:
data_dict = json.load(entry)
Json files with newlines
-
Inputting json file into python with
json.loads(_)
, -
Store each line as a dictionary in a list
1
[json.loads(jline) for jline in open(path, "r").read().split('\n')]
Write Json files
- Write each item in the list into the json file as a line
1
2
3
4
with open("write.jsonl", "w") as outfile:
for item in sample_list:
outfile.write(json.dumps(item))
outfile.write("\n")