Home > database >  JMESPath to return as json
JMESPath to return as json

Time:09-28

JMESPath can be used to query json but the return is no longer json any more:

E.g., searching {"a": "foo", "b": "bar", "c": "baz"} with JMESPath a will yield "foo".

How can I return {"a": "foo"} instead?

jq is able to do it:

curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq '.[0] | {message: .commit.message, name: .commit.committer.name}'

{
  "message": "Merge pull request #162 from stedolan/utf8-fixes\n\nUtf8 fixes. Closes #161",
  "name": "Stephen Dolan"
}

CodePudding user response:

See MultiSelect Hash. For example,

>>> import jmespath
>>> expression = jmespath.compile('{a: a}')
>>> expression.search({"a": "foo", "b": "bar", "c": "baz"})
{'a': 'foo'}
  • Related