I have written a Python script that outputs a long data structure (dictionary i called “celldict”) in Json format. Here’s a small part of it :
{
"1224": {
"OUT3FA_5": 12,
"IN1": 37,
"Total_IN1": 37
},
"1225": {
"OUT3FA_5": 24,
"IN1": 59,
"Total_IN1": 22
}
}
But what I would like to do is have something like this :
{
"success": true,
"data": [
{
"Week":"1224",
"OUT3FA_5": 65,
"IN1": 85,
"Total_IN1": 100
},
{
"Week":"1225",
"OUT3FA_5": 30,
"IN1": 40,
"Total_IN1": 120
}
]
}
Is there a way to format the json output with Python to get I what I want?
I do:
print json.dumps(celldict)
to get my output.
Any help would be much much appreciated.
Just put
celldictinside another dict:You’ll have to add the
Weekkey to thecelldictdictionaries first:or use create a copy of each dict on-the-fly:
The latter method, with some indentation, produces:
Reading between the lines, it seems as if the
1224and1225keys in your input example are actually the week numbers you are referring to. If so, they are easily incorporated:would produce: