python querying a json objectpath
Question: I've a nested json structure, I'm using objectpath (python API version), but I
don't understand how to select and filter some information (more precisely the
nested information in the structure).

EG. I want to select the "description" of the action "reading" for the user
"John".

JSON:

    
    
    {
        "user": 
        [
                "actions":   
                 [
                     {
                     "name": "reading",
                     "description": "blablabla"
                     }
                 ]
                "name": "John"
        ]
    }
    

CODE:

    
    
    $.user[@.name is 'John' and @.actions.name is 'reading'].(actions.description)
    

but it doesn't work (empty set but in my JSON it isn't so). Any suggestion?


Answer: Is this what you are trying to do?

    
    
    import objectpath
    
    data = {
        "user": {
            "actions": {
                    "name": "reading",
                    "description": "blablabla"
                },
            "name": "John"
        }
    }
    
    tree = objectpath.Tree(data)
    result = tree.execute("$.user[@.name is 'John'].actions[@.name is 'reading'].description")
    for entry in result:
        print entry
    

**Output**

    
    
    blablabla
    

I had to fix your JSON. Also, `tree.execute` returns a generator. You could
replace the `for` loop with `print result.next()`, but the `for` loop seemed
more clear.

