Last active
March 8, 2025 21:45
-
-
Save joar/776b7d176196592ed5d8 to your computer and use it in GitHub Desktop.
Add a field to an object with JQ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Add field | |
echo '{"hello": "world"}' | jq --arg foo bar '. + {foo: $foo}' | |
# { | |
# "hello": "world", | |
# "foo": "bar" | |
# } | |
# Override field value | |
echo '{"hello": "world"}' | jq --arg foo bar '. + {hello: $foo}' | |
{ | |
"hello": "bar" | |
} | |
# { | |
# "hello": "bar" | |
# } | |
# Concat and add | |
echo '{"hello": "world"}' | jq --arg foo bar '. + {foo: ("not" + $foo)}' | |
# { | |
# "hello": "world", | |
# "foo": "notbar" | |
# } |
@aborruso This seems to work:
jq 'to_entries | map( { (.key) : (.value + { key: .key }) }) | add'
thank you @MaxNanasy
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
I have this input
And I would like for each key, to add its value inside the related json object
If I run this
I obtain something similar, but I have the cartesian product
How to avoid it?
Thank you