LiLi Tam LiLi Tam - 1 month ago 10
JSON Question

(Python)How to process complicated json data

//Please excuse my poor English.

Hello everyone, I am doing a project which is about a facebook comment spider.
then I find the Facebook Graph GUI. It will return a json file that's so complicated for me.
The json file is include so many parts

then I use json.loads to get all the json code
finally it return a dict for me.
and i dont know how to access the Value

for example i want get all the id or comment.
but i can only get the 2 key of dict "data" and "pading"
so, how can i get the next key? like "id" or "comment"
and how to process this complicated data.

code

Thank you very much.

Answer Source

Two ways I can think of, either you know what you're looking for and access it directly or you loop over the keys, look at the value of the keys and nest another loop until you reach the end of the tree.

You can do this using a self-calling function and with the appropriate usage of jQuery.

Here is an example:

function get_the_stuff(url)
{
  $.getJSON(url, function ( data ) {
    my_parser(data) });
}

function my_parser(node)
{
  $.each(node, function(key, val) {
    if ( val && typeof val == "object" ) { my_parser(val); }
    else { console.log("key="+key+", val="+val); }
  });
}

I omitted all the error checking. Also make sure the typeof check is appropriate. You might need some other elseif's to maybe treat numbers, strings, null or booleans in different ways. This is just an example.

EDIT: I might have slightly missed that the topic said "Python"... sorry. Feel free to translate to python, same principles apply.

EDIT2: Now lets' try in Python. I'm assuming your JSON is already imported into a variable.

def my_parser(node, depth=0):
    if type(node) == "list":
        for val in node:
            my_parser(val,depth+1)
    elif type(node) == "dict":
        for key in node:
            printf("level=%i key=%s" % ( depth, key ))
            my_parser(node[key], depth+1)
    elif type(node) == "str":
            pritnf("level=%i value=%s" % ( depth, node ))
    elsif type(node) == "int":
            printf("level=%i value=%i % ( depth, node ))
    else:
            printf("level=%i value_unknown_type" % ( depth ))