Python JPath

A while ago I spent some time hacking on a little python library to query JSON or YAML objects paths using a language inspired in XPathPython JPath. I thought it could be useful to share it with people out there.

This allows for simple access to elements included in repetitive structures such as twitter feeds. I have very basic support for the language implemented (as I only need a few features).

Here's a little example of how it works:

a = {'a': [{'b': {'x': {'x': {'x': True}}}},{'b': True, 'a': False},{'b': "foo"}]}

BaseQuery ('/a/*/*').execute (a)    

This snippet executes the /a/*/* query (all childs of all childs of the value stored in the 'a' key of the root object) and returns a list of all matches:

[{'x': {'x': {'x': True}}}, False, True, 'foo']

I do not intend to do a 1-1 match of all the XPath language, only pick the features that makes sense in the JSON/YAML context. The project is hosted in github, merge requests are welcome!

Advertisements

2 thoughts on “Python JPath

  1. I’ve done a Python port of jsonpath in PyPI as “jsonpath”
    I also have a package called JSAM (for JSON Storage Access Method) that implements two XPath inspired query interfaces on a forest of JSON trees. One takes XPath like strings:
    /*/hh/*[hname = ‘Budne’]/person/*[first = ‘Phil’]
    And another, which I called P4J (after E4X) in which the same querylooks like:
    p = jsam.p4j.abs()
    p.ANY.hh.ANY.pred(r.hname == “Budne”).person.ANY.pred(r.first == “Phil”)
    Both queries can be executed using the “xpath” method on a JSAM object store (must be one which is backed by an SQL database — currently either SQLite or MySQL)

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s