I have a complex list (json file) and I am trying to flatten it in order to export it to a CSV file.
Some notes:
- The number of the objects in some nests may vary
- Some values can be null (empty)
- The data is a key, therefore I need to manipulate it just like I did a while ago in a previous question but in Javascript: LINK
My JSON is given below:
[
{
"masterName": "FirstOne",
"mainNames": [
{
"numbers": {},
"Name": "PlacedValue1",
"Type": "zzz"
},
{
"numbers": {
"2019-05-17T00:00:00Z": {
"NumberOne": 2.0,
"NumberTwo": 0.0
},
"2019-05-29T00:00:00Z": {
"NumberOne": 89153.0,
"NumberTwo": 18.0
},
"2019-05-30T00:00:00Z": {
"NumberOne": 14.0,
"NumberTwo": 0.0
}
},
"Name": "PlacedValue2",
"Type": "zzz"
},
{
"numbers": {
"2019-05-29T00:00:00Z": {
"NumberOne": 219737.0,
"NumberTwo": 85.0
},
"2019-05-30T00:00:00Z": {
"NumberOne": 261415.0,
"NumberTwo": 116.0
}
},
"Name": "PlacedValue3",
"Type": "zzz"
}
]
},
{
"masterName": "SecondOne",
"mainNames": [
{
"numbers": {
"2019-05-17T00:00:00Z": {
"NumberOne": 2.0,
"NumberTwo": 0.0
},
"2019-05-29T00:00:00Z": {
"NumberOne": 89153.0,
"NumberTwo": 18.0
}
},
"Name": "PlacedValue3",
"Type": "zzz"
},
{
"numbers": {
"2019-05-29T00:00:00Z": {
"NumberOne": 219737.0,
"NumberTwo": 85.0
}
},
"Name": "PlacedValue4",
"Type": "zzz"
}
]
}
]
I am trying to now focus on passing the dictionary key (date in this case) as an element in the equivalent dictionary. For instance, I want this:
"2019-05-17T00:00:00Z": {
"NumberOne": 2.0,
"NumberTwo": 0.0
}
to be:
{
"date" : "2019-05-17T00:00:00Z"
"NumberOne": 2.0,
"NumberTwo": 0.0
}
but that fails so far (comments in the code):
json_array2 = { "2019-05-19T00:00:00Z": { "one": 185, }, "2019-04-25T00:00:00Z": { "two": 207, } }
#The idea is to add the date as a new item in the dictionary as a start
for v in json_array2:
key = v
json_array['date'] = v
print(json_array2)