'Python: How to look up a specific key in json and add values to it?
I need to look for a key in json e add some info that come from a SQl query. The problem is how to look for this specific key and add the info. Here's a example:
{
"charts":{
"chart_file": "Z1.xml",
"chart_path":"...path_to_chart",
"modified":"date",
"chart_tags":{}
}
}
The query give me info about the "chart_tags" and initially its empty.
Assuming there are 3 different tags, I would like to add the information being the tag name the main key and inside it the information of colors, scales and ID. Here's a desired output:
{
"charts":{
"chart_file": "Z1.xml",
"chart_path":"...path_to_chart",
"modified":"date",
"chart_tags":{
"tagNAME":{
"tagID":1,
"scale":"[0,100]",
"color":"#000000"
},
"tagNAME2":{
"tagID":2,
"scale":"[0,5]",
"color":"#e3dc19"
},
"tagNAME3":{
"tagID":3,
"scale":"[20,80]",
"color":"#23fa67"
}
}
}
}
I've been trying to add the information in a dict and right after a json.dump, but I've been having problems with the dict not being hashable. Here's what i tryed and thanks for the help
jsonFile = open('teste.config', 'r', encoding='utf-8-sig')
millexcsMills = json.load(jsonFile)
currentMill=millexcsMills["mills"][millName]
for x, y,z,w in zip(df1.columns.tolist()[1:], chartTrendColors,chartTrendScales,chartTagID):
data = {
"tagName":x,
"color":y,
"scale": z,
"tagID":w,
}
millexcsMills['mills'][currentMill]['charts']['chart_tags'].append(data)
with open('teste.config', 'w') as outfile:
json.dump(jsonFile, outfile, indent=6)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
