I can work out how to use jq to replace a value from a variable,
$ jq -n --arg name bar '{"name":$name}'
{
"name": "bar"
}
But I am not sure how to replace multiple values.
{
...
"snstopic": {
"topic-project1": "team-project1-dev",
"topci-project2": "team-project2-dev",
... (different json files have different number of sns topics)
},
...
}
I set these environment variables:
$ export AWS_DEFAULT_REGION=us-east-2
$ export AWS_ACCOUNT_ID=123456789012
$ export ARN_PREFIX="arn:aws:sns:${AWS_DEFAULT_REGION}:${AWS_ACCOUNT_ID}:"
I want to get output as below
{
...
"snstopic": {
"topic-project1": "arn:aws:sns:us-east-2:123456789012:team-project1-dev",
"topci-project2": "arn:aws:sns:us-east-2:123456789012:team-project2-dev",
... (different json files have different number of sns topics
},
...
}
How to add it in all matched keys in .snstopic?
In a nutshell: map_values is your friend.
Let's suppose your template is in the file template.json. Then the following script will perform the specified transformation:
#!/bin/bash
# As far as this example is concerned,
# there is no need to export any variables
AWS_DEFAULT_REGION=us-east-2
AWS_ACCOUNT_ID=123456789012
ARN_PREFIX="arn:aws:sns:${AWS_DEFAULT_REGION}:${AWS_ACCOUNT_ID}:"
jq --arg prefix "$ARN_PREFIX" '
.snstopic |= map_values($prefix + .)
' template.json
template.json
{
"snstopic": {
"topic-project1": "team-project1-dev",
"topci-project2": "team-project2-dev"
}
}
Output:
{
"snstopic": {
"topic-project1": "arn:aws:sns:us-east-2:123456789012:team-project1-dev",
"topci-project2": "arn:aws:sns:us-east-2:123456789012:team-project2-dev"
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With