Skip to main content

A dev tool for performing queries on json objects.

Project description

treepath is a query language for selecting nodes from a json data-structure. The query expressions are similar to jsonpath and
Xpath, but are written in python syntax.

Solar System Sample Data

The sample data use by the examples in this README.

solar_system = { ... }

{
  "star": {
    "name": "Sun",
    "Equatorial diameter": 109.168,
    "planets": {
      "inner": [
        {
          "name": "Mercury",
          "Equatorial diameter": 0.383,
          "has-moons": false
        },
        {
          "name": "Venus",
          "Equatorial diameter": 0.949,
          "has-moons": false
        },
        {
          "name": "Earth",
          "Equatorial diameter": 1.000,
          "has-moons": true
        },
        {
          "name": "Mars",
          "Equatorial diameter": 0.532,
          "has-moons": true
        }
      ],
      "outer": [
        {
          "name": "Jupiter",
          "Equatorial diameter": 11.209,
          "has-moons": true
        },
        {
          "name": "Saturn",
          "Equatorial diameter": 9.449,
          "has-moons": true
        },
        {
          "name": "Uranus",
          "Equatorial diameter": 4.007,
          "has-moons": true
        },
        {
          "name": "Neptune",
          "Equatorial diameter": 3.883,
          "has-moons": true
        }
      ]
    }
  }
}

Typical example.

When working with json data-structures, there is a need to fetch specific pieces of data deep in the tree. A common approach to this problem is writing structural code. This approach can become quite complex depending on the json structure and search criteria.

A more declarative approach would be to use a query language as it does a better job at communicating the intent of what is being searched for.

Here are two examples that fetched the planet Earth from the sample solar-system data defined above. One is structural code, and the other is query declaration.

Structured Python Syntax declarative Python Syntax Using treepath
def get_planet(name, the_solar_system):
        try:
            inner = the_solar_system['star']['planets']['inner']
            for planet in inner:
                if name == planet.get('name', None):
                    return planet
        except KeyError:
            pass
        raise Exception(f"The planet {name} not found")

earth = get_planet('Earth', solar_system)
earth = get(path.star.planets.inner[wc][has(path.name == 'Earth')], solar_system)

Both examples will return the following results; however, the declarative approach uses only one line of code to construct the same search algorithm.

{'name': 'Earth', 'Equatorial diameter': 1.0, 'has-moons': True}

query example.

Description Xpath jsonpath treepath
Find planet earth. /star/planets/inner[name='Earth'] $.star.planets.inner[?(@.name=='Earth')] path.star.planets.inner[wc][has(path.name == 'Earth')]
List the names of the inner planets. /star/planets/inner[*].name $.star.planets.inner[*].name path.star.planets.inner[wc].name
List the names of all planets. /star/planets/*/name $.star.planets.[*].name path.star.planets.wc[wc].name
List the names of all the celestial bodies. //name $..name path.rec.name
List all nodes in the tree Preorder //* $.. path.rec
Get the third rock from the sun /star/planets/inner[3] $.star.planets.inner[2] path.star.planets.inner[2]
List first two inner planets /star/plnaets.inner[position()<3] $.star.planets.inner[:2] path.star.planets.inner[0:2]
$.star.planets.inner[0, 1] path.star.planets.inner[0, 2]
List planets smaller than earth /star/planets/inner[Equatorial_diameter < 1] $.star.planets.inner[?(@.['Equatorial diameter'] < 1)] path.star.planets.inner[wc][has(path["Equatorial diameter"] < 1)]
List celestial bodies that have planets. //*[planets]/name $..*[?(@.planets)].name path.rec[has(path.planets)].name

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

treepath-0.0.0.dev2.tar.gz (17.6 kB view hashes)

Uploaded Source

Built Distribution

treepath-0.0.0.dev2-py3-none-any.whl (25.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page