Search code examples
jsonjmespath

Change JSON key using 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?


Solution

  • After doing some testing I found the solution is by using this expression:

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