Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (12.7k points)

I have a settled JSON structure, I'm utilizing objectpath (python API rendition), yet I don't see how to choose and channel some data (all the more unequivocally the settled data in the structure). 

EG. I need to choose the "description" of the activity "reading" for the client "John".

JSON:

{

    "user": 

    {

            "actions":   

             [

                 {

                 "name": "reading",

                 "description": "blablabla"

                 }

             ]

            "name": "John"

    }

}

Code:

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

But, it's not working. Can anyone help me?

1 Answer

0 votes
by (26.4k points)

Check the following code:

 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

Here, tree.execute will actually returns generator. Moreover, you can replace for loop with print result.next(), but the for loop seems to be more clear. 

Are you interested to know python in detail? Come and join python certification course to gain more knowledge.

Watch this video tutorial on how to become a professional in python...

Related questions

0 votes
2 answers
asked Oct 4, 2019 in Python by Tech4ever (20.3k points)
0 votes
1 answer
asked Dec 10, 2020 in Python by laddulakshana (12.7k points)
0 votes
1 answer
0 votes
1 answer
asked Jul 15, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer

Browse Categories

...