Change JSON key using jmespath

时间:2017-07-12 08:17:15

标签: json jmespath

Is there any reason to change or transform JSON key to something else using jmespath?

For example if I have JSON like this:

[
{"topic_id": 123, "name": "Topic 1"},
{"topic_id": 234, "name": "Topic 2"}
]

how to change the "topic_id" to simply "id"? So the result will be like this:

[
{"id": 123, "name": "Topic 1"},
{"id": 234, "name": "Topic 2"}
]

I understand that it can be done using any language, but then the solution will be different for each language. I'd like to have an agnostic solution using jmespath.

From what I read in jmespath doc, it can be used to create a new JSON by filtering an existing JSON. Can the same technique used in my case?

1 个答案:

答案 0 :(得分:2)

在做了一些测试后,我发现解决方案是使用这个表达式:

[].{id: topic_id, name: name}