Skip to main content

JSON Matching Expressions

Project description

JMESPath

JMESPath (pronounced “jaymz 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”].

NOTE: jmespath is being actively developed. There are a number of features it does not currently support that may be added in the future.

Grammar

The grammar is specified using ABNF, as described in RFC4234

expression        = sub-expression / index-expression / or-expression / identifier / "*"
expression        =/ multi-select-list / multi-select-hash
sub-expression    = expression "." expression
or-expression     = expression "||" expression
index-expression  = expression bracket-specifier / bracket-specifier
multi-select-list = "[" ( non-branched-expr *( "," non-branched-expr ) ) "]"
multi-select-hash = "{" ( keyval-expr *( "," keyval-expr ) ) "}"
keyval-expr       = identifier ":" non-branched-expr
non-branched-expr = identifier /
                    non-branched-expr "." identifier /
                    non-branched-expr "[" number "]"
bracket-specifier = "[" (number / "*") "]" / "[]"
number            = [-]1*digit
digit             = "1" / "2" / "3" / "4" / "5" / "6" / "7" / "8" / "9" / "0"
identifier        = 1*char
identifier        =/ quote 1*(unescaped-char / escaped-quote) quote
escaped-quote     = escape quote
unescaped-char    = %x30-10FFFF
escape            = %x5C   ; Back slash: \
quote             = %x22   ; Double quote: '"'
char              = %x30-39 / ; 0-9
                    %x41-5A / ; A-Z
                    %x5F /    ; _
                    %x61-7A / ; a-z
                    %x7F-10FFFF

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.

Python Library

The included python implementation has two convenience 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 store the compiled expressions and reuse them to perform repeated searches:

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

You can also use the jmespath.parser.Parser class directly if you want more control.

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.2.0.tar.gz (12.3 kB view details)

Uploaded Source

File details

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

File metadata

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

File hashes

Hashes for jmespath-0.2.0.tar.gz
Algorithm Hash digest
SHA256 f2a2d56241ce57b447510b2a637838c4c80248275a5021c1e3eecb2138ed6c57
MD5 304d0dc541d2f20609523f4e24f5140f
BLAKE2b-256 6a55e79b9422223c21c3bcf1bf82a49e1501ddbbfa8e8da058fb9c90b9459669

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