3

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?

6
  • 1
    I don't think this is even valid JSON. Commented May 2, 2015 at 0:14
  • @PepperoniPizza: Yeah, he has a name: value inside an array. Commented May 2, 2015 at 0:26
  • 1
    Why is this tagged python? You're asking how to use some JavaScript library in JavaScript to parse some JSON. Commented May 2, 2015 at 0:27
  • 1
    I'm using the python version of the library and it has some features that the javascript version don't have. Commented May 2, 2015 at 12:33
  • There's a group where you can ask Qs about ObjectPath if something doesn't work as expected groups.google.com/forum/#!forum/objectpath Commented Oct 6, 2015 at 20:56

3 Answers 3

7

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.

Sign up to request clarification or add additional context in comments.

Comments

1
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')

2 Comments

Please include some explanation to make the example easier to understand. :-)
This does not answer the question and is even more confusing than the documentation (which is some of the worst I have ever seen).
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.