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,
"b":11
},
{
"id":1,
"e":44,
"c":77,
"f":55,
"d":66
},
{
"id":3,
"b":11,
"a":22
},
{
"id":3,
"d":44,
"c":88
}
]
File: output.json
[
{
"id":1,
"a":22,
"b":11,
"e":44,
"c":77,
"f":55,
"d":66
},
{
"id":3,
"b":11,
"a":22,
"d":44,
"c":88
}
]
Any pointers would be appreciated!
Example-1: Search key in simple JSON data Here, a variable named customerData is defined to store the JSON data. The value of the key will be taken as input from the user. loads() method of JSON module is used to load JSON data in the variable named customer. Next, 'in' operator is used to search the key.
As the loaded json data is just nested lists and dicts, you can use the ordinary list/dict operations; in particular, list comprehension is useful. Show activity on this post. import json # Loding the data pathToFile = "bb. json" with open(pathToFile, 'r') as file: content = file.
The json filter converts a JavaScript object into a JSON string. This filter can be useful when debugging your applications. The JavaScript object can be any kind of JavaScript object.
The idea is to:
json.load()
to load the JSON content from file to a Python listid
, using collections.defaultdict
and .update()
methodjson.dump()
to dump the result into the JSON fileImplementation:
import json
from collections import defaultdict
# read JSON data
with open("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 data
with open("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
}
]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With