Search code examples
bashinsertkeyjq

pass arguments to jq when inserting key not interpolated


When I run

jq --arg md "blablabla" '. +={'content': "$md"}' blog.json

in blog.json

{
"foo": "bar"
}

I get

{
  "foo": "bar",
  "content": "$md"
}

The desired output would be:

{
  "foo": "bar",
  "content": "blablabla"
}

Solution

  • The use of " around $md is causing JQ to interpret your input as a string literal instead of expanding the variables

    Access the variable directly without quotes

    jq --arg md "blablabla" '. + {"content": $md}' blog.json

    output

    {
      "foo": "bar",
      "content": "blablabla"
    }