How To Filter Json Data Using Python?
How to convert JSON data from input.json to output.json using Python? In general, what data structures are used for filtering JSON data? File: input.json [ { 'id':1, 'a':22
Solution 1:
The idea is to:
- use
json.load()
to load the JSON content from file to a Python list - regroup the data by the
id
, usingcollections.defaultdict
and.update()
method - use
json.dump()
to dump the result into the JSON file
Implementation:
import json
from collections import defaultdict
# read JSON datawithopen("input.json") as input_file:
old_data = json.load(input_file)
# regroup data
d = defaultdict(dict)
for item in old_data:
d[item["id"]].update(item)
# write JSON datawithopen("output.json", "w") as output_file:
json.dump(list(d.values()), output_file, indent=4)
Now the output.json
would contain:
[{"d":66,"e":44,"a":22,"b":11,"c":77,"id":1,"f":55},{"b":11,"id":3,"d":44,"c":88,"a":22}]
Solution 2:
from collections import defaultdict
input_list=[{"id":1, ...}, {...}]
result_dict=defaultdict(dict)
for d in input_list:
result_dict[d['id']].update(d)
output_list=result_dict.values()
result_dict
is a default dictionary which uses a dict
for every access without a available key. So we iterate through the input_list and update our result_dict with key equals id
with the new values from the corresponding dictionary.
The output list is a transformation of the result_dict
and uses only its values.
Use the json
module to work directly with the json data.
Post a Comment for "How To Filter Json Data Using Python?"