0

I have a .json file which I want to read, take all the contents.. and append it to a json string that's in a bash variable

input.json

[{
    "maven": {
        "coordinates": "somelib",
        "repo": "somerepo"
    }
},
{
    "maven": {
        "coordinates": "anotherlib",
        "exclusions": ["exclude:this", "*:and-all-that"]
    }
}]

OUR_BIG_JSON variable

{
    "name": "",
    "myarray": [
        {
            "generic": {
                "package": "somepymodule"
            }
        },
        {
            "rcann": {
                "package": "anothermodule==3.0.0"
            }
        }
    ],

and a json I want to append to.. it's residing in a variable so we must echo it as we use jq

command attempt

echo "starting..."
inputs_json=`cat input.json`

echo "$inputs_json"
echo "$OUR_BIG_JSON"

OUR_BIG_JSON=$(echo "${OUR_BIG_JSON}" |./jq '.myarray += [{"date":"today"}]')  # This worked.. 

next line fails

OUR_BIG_JSON=$(echo "${OUR_BIG_JSON}" |./jq '.myarray += ' $inputs_json)

out comes errors when trying to take the $inputs_json contents and just pipe it into the variable.. basically append to the JSON in memory..

jq: error: syntax error, unexpected INVALID_CHARACTER (Windows cmd shell quoting issues?) at <top-level>, line 1: .myarray += \"$inputs_json\" jq: 1 compile error

1
  • So you're making JSON by appending strings (??), and want JQ to append some more? Commented Jan 17, 2022 at 16:59

1 Answer 1

2

Using the --argjson option to read in the contents of your shell variable enables you to use it as variable inside the jq filter, too. Your input file can normally be read in via parameter.

jq --argjson v "${OUR_BIG_JSON}" '…your filter here using $v…' input.json

For example:

jq --argjson v "${OUR_BIG_JSON}" '$v + {myarray: ($v.myarray + .)}' input.json

Or by making the input too a variable inside the jq filter

jq --argjson v "${OUR_BIG_JSON}" '. as $in | $v | .myarray += $in' input.json

With the -n option you could also reference the input using input:

jq --argjson v "${OUR_BIG_JSON}" -n '$v | .myarray += input' input.json

And there's also the options --argfile and --slurpfile to read in the input file as variable directly...


As you have tagged bash you could also do it the other way around and use a herestring <<< to read in the bash variable as input and then using either --argfile or --slurpfile to reference the file content through a variable. For instance:

jq --argfile in input.json '.myarray += $in' <<< "${OUR_BIG_JSON}"
Sign up to request clarification or add additional context in comments.

3 Comments

@ikegami My jq (jq-1.6; Linux) does not accept simultaneous input from stdin (coming from <<<) and from parameter. Same goes for echo … | jq … input.json or echo … | jq … <<< "herestring" which just ignore stdin.
Yeah, was thinking of <( ... ). But that's not as simple anymore
@pmf big thanks, the first example worked brilliantly .. wow!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.