Andriy Ivaneyko
Andriy Ivaneyko

Reputation: 22021

Replacing variables in a JSON template using 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:

Upvotes: 4

Views: 1755

Answers (1)

oguz ismail
oguz ismail

Reputation: 50750

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"
}

Upvotes: 8

Related Questions