Navigating JSON in Python? -


answer: square brackets inside data["data"]["items"] indicates list of dictionaries. had thought brackets indicated of dictionaries list inside list of 1 item (i.e. [[item1, item2]]) had call 0 index so: data["data"]["items"][0]. incorrect, [0] unecessary, should have known, didn't realize.


i using gae, can think of self.response.write statement print.

for reason, although user_xid prints correctly, interpreter giving me error when try parse json_data["data"]["items"][0] in for loop, appears evaluating x string, because getting error telling me string indicies must integers.

i didn't realize json_data["data"]["items"][0] yield string. i'm stymied. have using json_data["data"]["items"][0] opposed json_data["data"]["items"]?

i have tried json.loads(json_data["data"]["items"][0]) no avail. input appreciated.

i have following json, have broken out bit make easier read:

 {"meta":     {"user_xid": "redacted", "message": "ok", "code": 200, "time": 1436273692}, "data":     {"items": [          {"time_completed": 1436193938, "xid": "4tlz2m3x8_e9mtpiqsqudrj-83spikha", "title": "for 7h 40m", "time_created": 1436165067, "time_updated": 1436194828, "details":              {"body": 0, "sound": 14348, "tz": "america/vancouver", "awakenings": 1, "light": 13258, "mind": 0, "asleep_time": 1436165760, "awake": 1265, "rem": 0, "duration": 28871, "smart_alarm_fire": 1436193000, "quality": 81, "awake_time": 1436193600, "sunrise": 1436184840, "sunset": 1436156340},          "date": 20150706, "shared": true, "sub_type": 0},          {"time_completed": 1436135400, "xid": "4tlz2m3x8_hk71lnuslbeq4ascwjg-j8", "title": "for 25m", "time_created": 1436133600, "time_updated": 1436135590, "details":               {"body": 0, "sound": 0, "tz": "america/vancouver", "awakenings": 0, "light": 1500, "mind": 0, "asleep_time": 1436133600, "awake": 300, "rem": 0, "duration": 1800, "smart_alarm_fire": 0, "quality": 2, "awake_time": 1436134800, "sunrise": 0, "sunset": 0},               "date": 20150705, "shared": true, "sub_type": 2},          {"time_completed": 1436133497, "xid": "4tlz2m3x8_giyf6g9_zkh9xrkpuezmjm", "title": "for 23m", "time_created": 1436131387, "time_updated": 1436135461, "details":              {"body": 0, "sound": 1391, "tz": "america/vancouver", "awakenings": 0, "light": 0, "mind": 0, "asleep_time": 1436132100, "awake": 719, "rem": 0, "duration": 2110, "smart_alarm_fire": 1436133445, "quality": 4, "awake_time": 1436133300, "sunrise": 0, "sunset": 0},          "date": 20150705, "shared": true, "sub_type": 1},          {"time_completed": 1436117080, "xid": "4tlz2m3x8_euzyqcoxyojo7hfe92esqg", "title": "for 26m","time_created": 1436115061, "time_updated": 1436121619, "details":              {"body": 0, "sound": 480, "tz": "america/vancouver", "awakenings": 0, "light": 1119, "mind": 0, "asleep_time": 1436115479, "awake": 420, "rem": 0, "duration": 2019, "smart_alarm_fire": 1436117069, "quality": 2, "awake_time": 1436116800, "sunrise": 1436098380, "sunset": 1436156280},          "date": 20150705, "shared": true, "sub_type": 1},           ],          "links":             {"next": "redacted"},   "size": 10}  } 

i using following in python:

json_data = json.loads(user_data)         self.response.write('<br><br>x_id: ' + json_data["meta"]["user_xid"])         = 0         x in json_data["data"]["items"][0]:             duration = x['details']['duration']             date = x["date"]             self.response.write('<br><br>sleep number ' + str(i) + ' | duration: ' + str(duration) + ' | date: ' + str(date))             += 1 

i using gae, can think of self.response.write statement print.

for reason, although user_xid prints correctly, interpreter giving me error when try parse json_data["data"]["items"][0] in for loop, appears evaluating x string, because getting error telling me string indicies must integers.

i didn't realize json_data["data"]["items"][0] yield string. i'm stymied. have using json_data["data"]["items"][0] opposed json_data["data"]["items"]?

i have tried json.loads(json_data["data"]["items"][0]) no avail. input appreciated.

thanks!

by doing basic debugging , data examination find use of [0] incorrect

i loaded data , tried

in [11]: in x['data']['items'][0]: ....:     print ....:      time_completed xid details title date shared sub_type time_created time_updated 

and got, not expected.

so going on? if you following

x['data']['items'][0] out[28]:  {'date': 20150706,  'details': {'asleep_time': 1436165760,   'awake': 1265,   'awake_time': 1436193600,   'awakenings': 1,   'body': 0,   'duration': 28871,   'light': 13258,   'mind': 0,   'quality': 81,   'rem': 0,   'smart_alarm_fire': 1436193000,   'sound': 14348,   'sunrise': 1436184840,   'sunset': 1436156340,   'tz': 'america/vancouver'},  'shared': true,  'sub_type': 0,  'time_completed': 1436193938,  'time_created': 1436165067,  'time_updated': 1436194828,  'title': 'for 7h 40m',  'xid': '4tlz2m3x8_e9mtpiqsqudrj-83spikha'} 

the result expect.

the answer because trying iterate on x['data']['items'][0]

when iterate on dictionary keys not values default. hence getting unexpected string.

where following code give want.

in [30]: in x['data']['items']:             print i['details']['duration']    ....:         28871    1800    2110    2019 

this iterates on each item (a dictioanary) in list of items.

i suggest downvote due large posting of code, , no real evidence of basic debugging.


Comments

Popular posts from this blog

How to provide Authorization & Authentication using Asp.net, C#? -

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

android - Pass an Serializable object in AIDL -