Prompt me, please, how to normalize this JSON file using Python?
This question is related to the previous
The current JSON contains:
{"total_stats": [{"domain": "domain.com","uptime": "100"},{"domain": "domain.com","threats": "345.01111783804436"}]
}
Desirable
{"total_stats": [{"domain": "domain.com","uptime": "100","threats": "345.01111783804436"}]
}
If you want to merge the dictionaries according the "domain"
key you can use (note: if dictionaries have common keys, the last dictionary value will be used):
dct = {"total_stats": [{"domain": "domain.com", "uptime": "100"},{"domain": "domain.com", "threats": "345.01111783804436"},]
}out = {}
for d in dct["total_stats"]:out.setdefault(d["domain"], {}).update(d)dct["total_stats"] = list(out.values())
print(dct)
Prints:
{"total_stats": [{"domain": "domain.com","uptime": "100","threats": "345.01111783804436",}]
}