Home > Back-end >  Error jq: invalid JSON text passed to --argjson
Error jq: invalid JSON text passed to --argjson

Time:05-09

Consider

$ keys='[["key1","a"],["key2","b"]]' jq -c --argjson keys "$keys" '.[] | [getpath($keys[])]' <<<$'[{"key1":{"a":1},"key2":{"b":2}}] [{"key1":{"a":3},"key2":{"b":4}}]'

I expect output

[1,2]
[3,4]

But I seem to doing something wrong when defining argjson. I get error:

jq: invalid JSON text passed to --argjson

What am I doing wrong? Thanks!

CodePudding user response:

You are either missing a semi-colon (or equivalent), or misunderstanding shell variables.

  1. With the semicolon:
keys='[["key1","a"],["key2","b"]]' ; jq -c -n --argjson keys "$keys" '$keys'
  1. Using shell variables:
keys='[["key1","a"],["key2","b"]]' jq -c -n 'env.keys | fromjson'
  • Related