jq & bash: make JSON array from variable

Once you have your variable loaded, you should use the split filter to split that string into an array.

$ jq -n --arg inarr "${ARR}" '{ arr: $inarr | split("\n") }'

In jq 1.3 and up you can use the --arg VARIABLE VALUE command-line option:

jq -n --arg v "$VAR" '{"foo": $v}'

I.e., --arg sets a variable to the given value so you can then use $varname in your jq program, and now you don't have to use shell variable interpolation into your jq program.

EDIT: From jq 1.5 and up, you can use --argjson to pass in an array directly, e.g.

jq -n --argjson v '[1,2,3]' '{"foo": $v}'

Tags:

Arrays

Bash

Json

Jq