Chris Cris
Chris Cris

Reputation: 215

python querying a json objectpath

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?

Upvotes: 3

Views: 10300

Answers (3)

admarple
admarple

Reputation: 36

I believe you're just missing a comma in JSON:

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

Assuming there is only one "John", with only one "reading" activity, the following query works:

$.user[@.name is 'John'].actions[0][@.name is 'reading'][0].description

If there could be multiple "John"s, with multiple "reading" activities, the following query will almost work:

$.user.*[@.name is 'John'].actions..*[@.name is 'reading'].description

I say almost because the use of .. will be problematic if there are other nested dictionaries with "name" and "description" entries, such as

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

To get a correct query, there is an open issue to correctly implement queries into arrays: https://github.com/adriank/ObjectPath/issues/60

Upvotes: 1

user3657941
user3657941

Reputation:

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.

Upvotes: 7

fndg87
fndg87

Reputation: 331

import objectpath import *

your_json = {"name": "felix", "last_name": "diaz"}

# This json path will bring all the key-values of your json

your_json_path='$.*'

my_key_values = Tree(your_json).execute(your_json_path)

# If you want to retrieve the name node...then specify it. 
my_name= Tree(your_json).execute('$.name')

# If you want to retrieve a the last_name node...then specify it. 
last_name= Tree(your_json).execute('$.last_name')

Upvotes: 2

Related Questions