Writing nested dictionaries in python for json.dump -


i have set of python lists want serially write in json file in structure 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 each of variables ('y','x','angle','speed' etc.). , have created nested dictionaries in python write these lists via loop. code follows

 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 following result. not able figure out 1) how iteratively populate dictionary in similar structure 2) adding values key 'timestamp'# 3) creating key - 'id'

{ "timestamp": {     "tracedata": {         "data": {             "x": "7.739439",             "speed": "6.072069",             "y": "49.421938",             "objecttype": "bus",             "angle": "68.576206"         }     } } } 

thank help

i assuming wanted, since can't have dict {} holding values 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, list [] of dicts {} (#1), each identifying "timestamp", , "tracedata" list [] contains dicts {} (#2), each of contains "id", , "data".

your code:

 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 @ single dict (of type #1), , several errors:

  1. "timestamp" should identifier of trace frame, integer or string, not dict {}, access dict anyways.
  2. "tracedata" list [], need access such - might contain more 1 dict {} (#2), in fact in example.
  3. you access "data" dict , write relevant information, once again, 1 dict #1 , 1 dict #2, result appears contain 1 of each.

this how should accessed when built, more or less, can idea how fix code:

for traceframe in traceframes:    print(traceframe["timestamp"])    tracedata in traceframe["tracedata"]:       print(tracedata["id"], " --> ", tracedata["data"]) 

Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -