Skip to main content

JSON Matching Expressions

Project description

JMESPath

https://badges.gitter.im/JoinChat.svg https://secure.travis-ci.org/jmespath/jmespath.py.png?branch=develop

JMESPath (pronounced “james path”) allows you to declaratively specify how to extract elements from a JSON document.

For example, given this document:

{"foo": {"bar": "baz"}}

The jmespath expression foo.bar will return “baz”.

JMESPath also supports:

Referencing elements in a list. Given the data:

{"foo": {"bar": ["one", "two"]}}

The expression: foo.bar[0] will return “one”. You can also reference all the items in a list using the * syntax:

{"foo": {"bar": [{"name": "one"}, {"name": "two"}]}}

The expression: foo.bar[*].name will return [“one”, “two”]. Negative indexing is also supported (-1 refers to the last element in the list). Given the data above, the expression foo.bar[-1].name will return “two”.

The * can also be used for hash types:

{"foo": {"bar": {"name": "one"}, "baz": {"name": "two"}}}

The expression: foo.*.name will return [“one”, “two”].

API

The jmespath.py library has two functions that operate on python data structures. You can use search and give it the jmespath expression and the data:

>>> import jmespath
>>> path = jmespath.search('foo.bar', {'foo': {'bar': 'baz'}})
'baz'

Similar to the re module, you can use the compile function to compile the JMESPath expression and use this parsed expression to perform repeated searches:

>>> import jmespath
>>> expression = jmespath.compile('foo.bar')
>>> expression.search({'foo': {'bar': 'baz'}})
'baz'
>>> expression.search({'foo': {'bar': 'other'}})
'other'

This is useful if you’re going to use the same jmespath expression to search multiple documents. This avoids having to reparse the JMESPath expression each time you search a new document.

Specification

If you’d like to learn more about the JMESPath language, you can check out the JMESPath tutorial. Also check out the JMESPath examples page for examples of more complex jmespath queries.

The grammar is specified using ABNF, as described in RFC4234. You can find the most up to date grammar for JMESPath here.

You can read the full JMESPath specification here.

Testing

In addition to the unit tests for the jmespath modules, there is a tests/compliance directory that contains .json files with test cases. This allows other implementations to verify they are producing the correct output. Each json file is grouped by feature.

Discuss

Join us on our Gitter channel if you want to chat or if you have any questions.

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

jmespath-0.7.1.tar.gz (19.7 kB view details)

Uploaded Source

Built Distribution

jmespath-0.7.1-py2.py3-none-any.whl (19.8 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file jmespath-0.7.1.tar.gz.

File metadata

  • Download URL: jmespath-0.7.1.tar.gz
  • Upload date:
  • Size: 19.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for jmespath-0.7.1.tar.gz
Algorithm Hash digest
SHA256 cd5a12ee3dfa470283a020a35e69e83b0700d44fe413014fd35ad5584c5f5fd1
MD5 ca76cb014165306c1eded212cfb78cf5
BLAKE2b-256 825c8439a7719f598449120a63f9f2f23423577d7a49374f927e7b5be338524f

See more details on using hashes here.

Provenance

File details

Details for the file jmespath-0.7.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for jmespath-0.7.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 cb99b58dcf853f791bec28aa016c121663b79ee6125c892297d773d7fd4dcecf
MD5 1977b145d6923911ee1088d0b0221b8a
BLAKE2b-256 1ce7c7381144a6c96d2641ab46c69384c02dcb491d8c8f7fc90cfb97cfdbe7cd

See more details on using hashes here.

Provenance

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