1

I have a json which looks likes this:

[
  {"First Name": "xyz", "Last Name": "abc", "Name": "name1"},
  {"First Name": "abc", "Last Name": "xyz", "Name": "name2"},
  {"First Name": "abcd", "Last Name": "wxyz", "Name": "name3"}
]

I am able to easily extract entries which do not have spaces in them. E.g.:

[?Name=='name1']

Which gives:

[
  { "First Name": "xyz", "Last Name": "abc", "Name": "name1" }
]

However, I am unable to get entries where the keys have spaces in them. e.g.:

  • [?First Name =='xyz'] results in Syntax error: expected Rbracket, got: UnquotedIdentifier
  • [?'First Name' =='xyz'] results in []

Am I missing something here?

JMESPath version used in Python:

>>> jmespath.__version__
'0.10.0'
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83

1 Answers1

2

The key in the query should be in double quotes:

[?"First Name" == 'xyz']

This is because single quotes and doubles quotes do not have the same meaning in JMESPath.

  • single quotes are raw string literals, so, if you are using them you are trying to assess that the string First Name is equal to the string xyz, which will, obviously never ever be true

  • double quotes on the other hand are for identifiers:

    An identifier can also be quoted. This is necessary when an identifier has characters not specified in the unquoted-string grammar rule. In this situation, an identifier is specified with a double quote, followed by any number of unescaped-char or escaped-char characters, followed by a double quote. The quoted-string rule is the same grammar rule as a JSON string, so any valid string can be used between double quoted, include JSON supported escape sequences, and six character unicode escape sequences.

    Source: https://jmespath.org/specification.html#identifiers

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83