How to use sed with jq to replace _ in key name with symbol a
{ "product_name":"kl" }
should become
{ "productaname":"kl" }
in a bash script
No need for sed
; it's easy to do in just jq
:
$ jq '{ productaname: .product_name }' <<<'{ "product_name":"kl" }'
{"productaname":"kl"}
If you want to replace underscores with a's in all keys of an object:
$ jq 'with_entries(.key |= gsub("_"; "a"))' <<<'{ "product_name":"kl", "foo_bar":12 }'
{"productaname":"kl","fooabar":12}
From the documentation for with_entries
:
to_entries, from_entries, with_entries
These functions convert between an object and an array of key-value pairs. If
to_entries
is passed an object, then for each k: v entry in the input, the output array includes{"key": k, "value": v}
.
from_entries
does the opposite conversion, andwith_entries(foo)
is a shorthand forto_entries | map(foo) | from_entries
to_entries | map(foo) | from_entries
to_entries | map(foo) | from_entries
, useful for doing some operation to all keys and values of an object.from_entries
accepts key, Key, name, Name, value and Value as keys.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.