Dynamic/computed keys in JMESPath?

≯℡__Kan透↙ 提交于 2019-12-05 05:09:13

Problem

  • How to construct a Jmespath query that returns objects with arbitrary key-value pairs
  • The keys need to be dynamic, based on the output of a jmespath filter expression

Workaround

  • As of this writing (2019-03-22), dynamic keys are not available in standard Jmespath
  • However, it is possible to return a list of lists instead of a list of objects, and simply post-process that list of lists outside of jmespath

Example

 [*].[@.name,@.age]

Returns

[['foo', 43], ['bar', 55]]

Which can then be post-processed outside of Jmespath, if that is an option for you.

See also

To get this result precisely: { foo: 43, bar: 55 } , you should use this query:

@.{foo: @[0].age, bar: @[1].age}

But as you can see I don't retrieve the keys foo and bar dynamically because I can't do it in JMES Path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!