简体   繁体   中英

Writing nested dictionaries in python for json.dump

I have a set of python lists which I want to serially write in a json file in a structure as given below

[

{
    "_id": {
        "$oid": "5707b5f4e4b0c4265caf3c87"
    },
    "TimeStamp": 1,
    "TraceData": [
        {
            "data": {
                "y": 443.732,
                "angle": 1.11416,
                "speed": 1.42906,
                "ObjectType": "Pedestrians",
                "x": 217.991,
                "D2D": "DUE_1_2"
            },
            "id": "DUE_1_1"
        },
        {
            "data": {
                "y": 571.965,
                "angle": 1.22555,
                "speed": 1.18132,
                "ObjectType": "Pedestrians",
                "x": 205.708,
                "D2D": "DUE_20_1"
            },
            "id": "DUE_20_2"
        }
    ]
},
{
    "_id": {
        "$oid": "5707b5a8e4b0a37fb1a38c57"
    },
    "TimeStamp": 2,
    "TraceData": [
        {
            "data": {
                "y": 419.936,
                "angle": 1.21995,
                "speed": 1.38648,
                "ObjectType": "Pedestrians",
                "x": 153.693,
                "D2D": "DUE_1_2"
            },
            "id": "DUE_1_1"
        },
        {
            "data": {
                "y": 571.143,
                "angle": 1.0939,
                "speed": 1.31394,
                "ObjectType": "Pedestrians",
                "x": 295.097,
                "D2D": "DUE_20_1"
            },
            "id": "DUE_20_2"
        }
    ]
}

]

I have python lists for each of the variables ('y','x','angle','speed' etc.). and I have created nested dictionaries in python to write these lists via a FOR loop. The code is as follows

 for eachdata in range(index-1): 
    OuterDict['TimeStamp']['TraceData']['data']['x'] = lat[eachdata]
    OuterDict['TimeStamp']['TraceData']['data']['y'] = long[eachdata]
    OuterDict['TimeStamp']['TraceData']['data']['angle'] = angle[eachdata]
    OuterDict['TimeStamp']['TraceData']['data']['speed'] = speed[eachdata]
    OuterDict['TimeStamp']['TraceData']['data']['ObjectType'] = ObjectType[eachdata]
index = 0


out_file = open("klsimulationjson.js","w")

json.dump(OuterDict,out_file,indent = 4)

out_file.close()

This code produces the following result. I am not able to figure out 1) how to iteratively populate the dictionary in the similar structure 2) adding values to the key 'TimeStamp'# 3) creating the key - 'id'

{
"TimeStamp": {
    "TraceData": {
        "data": {
            "x": "7.739439",
            "speed": "6.072069",
            "y": "49.421938",
            "ObjectType": "Bus",
            "angle": "68.576206"
        }
    }
}
}

Thank you for your help

I am assuming this is what you wanted, since you can't have a dict {} holding values with no keys:

[{
    "TimeStamp": 1,
    "TraceData": [{
        "data": {
            "y": 443.732,
            "angle": 1.11416,
            "speed": 1.42906,
            "ObjectType": "Pedestrians",
            "x": 217.991,
            "D2D": "DUE_1_2"
        },
        "id": "DUE_1_1"
    }, {
        "data": {
            "y": 430.645,
            "angle": 1.07287,
            "speed": 1.41977,
            "ObjectType": "Pedestrians",
            "x": 234.104,
            "D2D": "DUE_1_1"
        },
        "id": "DUE_1_2"
    }, {
        "data": {
            "y": 362.25,
            "angle": 1.43214,
            "speed": 1.44059,
            "ObjectType": "Pedestrians",
            "x": 50.5509,
            "D2D": "DUE_2_2"
        },
        "id": "DUE_2_1"
    }]
}, {
    "TimeStamp": 2,
    "TraceData": [{
        "data": {
            "y": 443.732,
            "angle": 1.11416,
            "speed": 1.42906,
            "ObjectType": "Pedestrians",
            "x": 217.991,
            "D2D": "DUE_1_2"
        },
        "id": "DUE_1_1"
    }, {
        "data": {
            "y": 430.645,
            "angle": 1.07287,
            "speed": 1.41977,
            "ObjectType": "Pedestrians",
            "x": 234.104,
            "D2D": "DUE_1_1"
        },
        "id": "DUE_1_2"
    }, {
        "data": {
            "y": 362.25,
            "angle": 1.43214,
            "speed": 1.44059,
            "ObjectType": "Pedestrians",
            "x": 50.5509,
            "D2D": "DUE_2_2"
        },
        "id": "DUE_2_1"
    }]
}]

So, basically a list [] of dicts {} (#1), each with an identifying "TimeStamp", and a "TraceData" list [] that also contains dicts {} (#2), each of which contains an "id", and "data".

Your code:

 for eachdata in range(index-1): 
    OuterDict['TimeStamp']['TraceData']['data']['x'] = lat[eachdata]
    OuterDict['TimeStamp']['TraceData']['data']['y'] = long[eachdata]
    OuterDict['TimeStamp']['TraceData']['data']['angle'] = angle[eachdata]
    OuterDict['TimeStamp']['TraceData']['data']['speed'] = speed[eachdata]
    OuterDict['TimeStamp']['TraceData']['data']['ObjectType'] = ObjectType[eachdata]

Looks at a single dict (of type #1), and then does several errors:

  1. "TimeStamp" should be an identifier of the Trace frame, like an integer or a string, not a dict {} , but you access it as a dict anyways.
  2. "TraceData" is a list [] , so you need to access it as such - it might contain more than one dict {} (#2), in fact it does in your example.
  3. You access the "data" dict and write the relevant information, but once again, you do it for just one dict #1 and just for one dict #2, so the result appears to only contain one of each.

This is how it should be accessed when properly built, more or less, so you can get an idea how to fix your code:

for traceFrame in traceFrames:
   print(traceFrame["TimeStamp"])
   for traceData in traceFrame["TraceData"]:
      print(traceData["id"], " --> ", traceData["data"])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM