I'm trying to write new fields and their values to an existing JSON object using Python, line by line, but it keeps creating another key instead. Here is a sample code :
# my object
data_set = {'fruits':[]}
fruit_state = 'eatable'
# the values i want to insert line by line into the same JSON object
data_set['fruits'].append({'apple' : fruit_state})
data_set['fruits'].append({'strawberry' : fruit_state})
data_set['fruits'].append({'lemon' : fruit_state})
json_dump = json.dumps(data_set, indent=4)
print(json_dump)
quit()
Which gives me :
{
"fruits": [
{
"apple": "eatable"
},
{
"strawberry": "eatable"
},
{
"lemon": "eatable"
}
]
}
But I'm trying to get something so the result would be :
{
"fruits": [
{
"apple": "eatable",
"strawberry": "eatable",
"lemon": "eatable"
}
]
}
I could put every value in an array and pass it, but I really want to insert the values line by line. Would you have an idea ? Thanks in advance.
fruitsto be a list containing a single dictionary? It would make more sense for it to just be a dictionary directly, i.e.{"fruits": {"apple": ..etc..} }