My first json query attempt is failing...

280 views
Skip to first unread message

Nicholas A

unread,
Feb 16, 2016, 12:43:12 PM2/16/16
to ObjectPath
Not having much experience with Python, I quickly realized I needed something to query json blobs.  I found this library, but cannot get anything to work, except the simplest of examples (which don't actually help me since basic json support already has).

I have a valid json schema (with elements removed)
{
"schemaVersion": "caas-api-v2",
"endpoints": [{
"type": "version",
"url": "/version",
"description": "Returns version string"
}, {
"type": "profile",
"url": "/api/v2/profile",
"description": "Returns information about the profile"
}, {
"type": "create-something",
"url": "/api/v2/something",
"description": "Create something POST endpoint"
}]
}

1) when I try 
        tree = Tree(myData['endpoints'])  # where myData contains the whole schema above
        path = tree.execute("$endpoints")

I get back the endpoints object.  Great...

2) when I try 
        path = tree.execute("$endpoints")[1]

I get back the 2nd object in endpoints.  Great... 

3) when I try
        path = tree.execute("$endpoints[@.type is 'version']")

I get back <generator object exeSelector at 0x00000000030A9630> 
Is this an error that the object can't be found?  If so why?
I expected to get the entire object that contains an element whose type key is set to the value version.  What am I missing?

thanks for any help... - Nicholas

Nicholas A

unread,
Feb 16, 2016, 2:25:57 PM2/16/16
to ObjectPath
And even stranger yet,

path = tree.execute("$[@.type is 'create-something']") is returning the whole json tree, not  

Adrian Kalbarczyk

unread,
Feb 17, 2016, 1:43:25 AM2/17/16
to ObjectPath

First you need to use dot ($.name) after $. Its because you could also use recursive descent operator like $..type. Second, ObjectPath often returns generator type. Wrap it with list(...) function and you're good to go!


Best, 
Adrian

Sent from Android.

Adrian Kalbarczyk

unread,
Feb 17, 2016, 1:44:46 AM2/17/16
to ObjectPath
Use dot operator followed by wildcard to get what you need:

path = tree.execute("$.*[@.type is 'create-something']") is returning the whole json tree, not  
Reply all
Reply to author
Forward
0 new messages