Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing variables in a JSON template using JQ

Tags:

json

templates

jq

I want to populate json template with value "Hello Jack", but the "Hello" part shall remain inside of template, is there are any way of doing that, I've tried code below but it gives me error:

jq -n --arg person "Jack" '{my_key: "Hello "$person}'
jq: error: syntax error, unexpected '$', expecting '}' (Unix shell quoting issues?) at <top-level>, line 1:
like image 385
Andriy Ivaneyko Avatar asked Oct 19 '25 15:10

Andriy Ivaneyko


1 Answers

Use string interpolation syntax like so:

jq -n --arg person Jack '{my_key: "Hello \($person)"}'

And to load the template from a file, use the -f switch:

$ cat template.json
{
  "my_key": "Hello \($person)"
}
$ jq -n --arg person Jack -f template.json
{
  "my_key": "Hello Jack"
}
like image 162
oguz ismail Avatar answered Oct 21 '25 07:10

oguz ismail



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!