How is it possible to rename a key ?
Say I have :
'{"long_message" : "Foo bar baz"}'
and I'd like to have
'{"msg" : "Foo bar baz"}'
Is this achievable with jq ?
Suppose you have an arbitrary object, obj, which is known to contain a key, "from", and is also known not to contain the key "to". To rename "from" to "to", you could use the filter:
. + {"to": .from} | del(.from)
This could of course be abstracted into a new jq filter.
To rename a key programmatically, you could use with_entries, e.g.
with_entries( if .key | contains("-") then .key |= sub("-";".") else . end)
See also the Q: How can I rename the keys of an object in the jq FAQ
Thanks !
Most helpful comment
Suppose you have an arbitrary object, obj, which is known to contain a key, "from", and is also known not to contain the key "to". To rename "from" to "to", you could use the filter:
This could of course be abstracted into a new jq filter.
To rename a key programmatically, you could use with_entries, e.g.
See also the Q: How can I rename the keys of an object in the jq FAQ