I can't figure out how to loop though a JSON object that is deeper than 1 level. The object is:
{
"data":[
{
"id":"251228454889939/insights/page_fan_adds_unique/day",
"name":"page_fan_adds_unique",
"period":"day",
"values":[
{
"value":9,
"end_time":"2012-05-29T07:00:00+0000"
},
{
"value":5,
"end_time":"2012-05-30T07:00:00+0000"
}
],
"title":"Daily New Likes",
"description":"Daily The number of new people who have liked your Page (Unique Users)"
},
{
"id":"251228454889939/insights/page_fan_adds/day",
"name":"page_fan_adds",
"period":"day",
"values":[
{
"value":9,
"end_time":"2012-05-29T07:00:00+0000"
},
{
"value":5,
"end_time":"2012-05-30T07:00:00+0000"
}
],
"title":"Daily New Likes",
"description":"Daily The number of new people who have liked your Page (Total Count)"
}
]
}
Code:
def parseJsonData(data):
output_json = json.loads(data)
for i in output_json:
print i
for k in output_json[i]:
print k
How come I can't access the object like: output_json[data][id]
?
I get an error if I try this:
string indice must be an integer
2 Answers 2
Being that your "data" key is actually a list of objects, you cannot access the items by their "id" field directly. You would need to access each item by a list index such as:
output_json["data"][0]["id"]
Now, if what you want to do is to be able to index the members of "data" by the "id" field as the key, you could reformat your data:
# make "data" a dict {id: item, }, instead of list [item1, item2, ...]
output_json['data'] = dict((item['id'], item) for item in json_data['data'])
print output_json['data']
# {'251228454889939/insights/page_fan_adds_unique/day': ...
print output_json['data']['251228454889939/insights/page_fan_adds_unique/day']
# {'description': 'Daily The number of new p ...
# ways to loop over "data"
for id_, item in output_json['data'].iteritems():
print id_, item
for item in output_json['data'].itervalues():
print item
Otherwise what you have to do is just loop over "data", since there is no real correlation between the index and the object:
for item in output_json["data"]:
print item['id']
# 251228454889939/insights/page_fan_adds_unique/day
# 251228454889939/insights/page_fan_adds/day
What you pasted is not valid JSON. There is an unmatched [ after "data".
Based on this, I would guess that maybe the data isn't what you think it is. If the value of output_json[data] is a list, then you won't be able to access output_json[data][id]
. Instead you'll have to do something like output_json[data][0][id]
, where the [0] accesses the first item in the list.
-
Im sure its valid json for the OP, but the OP just posted a slice of the whole thingjdi– jdi2012年06月01日 19:18:10 +00:00Commented Jun 1, 2012 at 19:18
-
Based on the indentation and bracketing, it doesn't look like it. There is an open bracket at the top that is apparently closed by a bracket at the very bottom, but in between those, the unmatched [ occurs. It's possible it's a slice, but if so it's a discontinous or otherwise malformed slice.BrenBarn– BrenBarn2012年06月01日 19:21:03 +00:00Commented Jun 1, 2012 at 19:21
-
All you have to do is add a closing
]
before the last}
. Indentation is not important.jdi– jdi2012年06月01日 19:22:12 +00:00Commented Jun 1, 2012 at 19:22 -
I realize the identation doesn't matter for the JSON. I'm just saying that in normal JSON output intended for human readers, the indentation is usually consistent, so the fact that it's not here suggests it wasn't a simple slice of the real data. In any case, it's difficult to answer the question if the OP isn't posting valid data, for this very reason: we have to puzzle out what the problem with the real data is by guessing about which parts of the pasted data are and are not part of the real data.BrenBarn– BrenBarn2012年06月01日 19:27:36 +00:00Commented Jun 1, 2012 at 19:27
-
I think you are missing the problem. The data is valid aside from him missing a single
]
, and the indent issue is most likely the OP being new to StackOverflow and not quite having a handle on code formatting. Its quite easy to see what the code is supposed to be. What is slightly ambiguous is really how he wants to access it.jdi– jdi2012年06月01日 19:31:57 +00:00Commented Jun 1, 2012 at 19:31