Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error jq: invalid JSON text passed to --argjson

Tags:

json

jq

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!

like image 573
Logan Lee Avatar asked Nov 15 '25 17:11

Logan Lee


1 Answers

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'
like image 145
peak Avatar answered Nov 17 '25 08:11

peak