Home > other >  How to replace values in a JSON dictionary with their respective shell variables in jq?
How to replace values in a JSON dictionary with their respective shell variables in jq?

Time:12-04

I have the following JSON structure:

{
  "host1": "$PROJECT1",
  "host2": "$PROJECT2",
  "host3" : "xyz",
  "host4" : "$PROJECT4"
}

And the following environment variables in the shell:

PROJECT1="randomtext1"
PROJECT2="randomtext2"
PROJECT4="randomtext3"

I want to check the values for each key, if they have a "$" character in them, replace them with their respective environment variable(which is already present in the shell) so that my JSON template is rendered with the correct environment variables.

I can use the --args option of jq but there are quite a lot of variables in my actual JSON template that I want to render.

I have been trying the following:

jq 'with_entries(.values as v | env.$v)

Basically making each value as a variable, then updating its value with the variable from the env object but seems like I am missing out on some understanding. Is there a straightforward way of doing this?

CodePudding user response:

You need to export the Bash variables to be seen by jq:

export PROJECT1="randomtext1"
export PROJECT2="randomtext2"
export PROJECT4="randomtext3"

Then you can go with:

jq -n 'with_entries((.value | select(startswith("$"))) |= env[.[1:]])'

and get:

{
  "host1": "randomtext1",
  "host2": "randomtext2",
  "host3": "xyz",
  "host4": "randomtext3"
}

CodePudding user response:

Exporting a large number of shell variables might not be such a good idea and does not address the problem of array-valued variables. It might therefore be a good idea to think along the lines of printing the variable=value details to a file, and then combining that file with the template. It’s easy to do and examples on the internet abound and probably here on SO as well. You could, for example, use printf like so:

printf "%s\t" ${BASH_VERSINFO[@]}
3   2   57  1   

You might also find declare -p helpful.

See also https://github.com/stedolan/jq/wiki/Cookbook#arbitrary-strings-as-template-variables

  • Related