I have JSON File that has this format
{
"links": [
{"source":"0","target":"1","weight":1,"color":"white"},
{"source":"0","target":"2","weight":1,"color":"yellow"},
{"source":"0","target":"3","weight":1,"color":"white"},
]
}
I want to collect all target for a single source like this:
{"source": 0, "neighbors": ["1","2","3"]} where neighbors are all the collected target
Here's my Code
import json
with open("linksGr.json") as file:
data = json.load(file)
collectDict = {}
for obj in data["links"]:
if (collectDict["source"] == obj["source"]):
collectDict["neighbour"] = obj["target"]
I just need a way to accumulate all targets for each source instead of there being multiple sources as I have done here
collectDict["source"] = obj["source"]
collectDict["neighbour"] = obj["target"]
Any help would be appreciated a lot. I am sure there is some basic concept and a simple way that I am missing here. Thanks for the help.