简体   繁体   中英

Passing a path (“key1.key2”) from a bash variable to jq

I am having trouble accessing bash variable inside 'jq'. The snippet below shows my bash loop to check for missing keys in a Json file.

#!/bin/sh
for key in "key1" "key2.key3"; do
  echo "$key"
  if ! cat ${JSON_FILE} | jq --arg KEY "$key" -e '.[$KEY]'; then
    missingKeys+=${key}
  fi
done

JSON_FILE:

{
  "key1": "val1",
  "key2": {
    "key3": "val3"
  }
}

The script works correctly for top level keys such as "key1". But it does not work correctly (returns null) for "key2.key3".

'jq' on the command line does return the correct value

cat input.json | jq '.key2.key3'
"val3"

I followed answers from other posts to come to this solution. However can't seem to figure out why it does not work for nested json keys.

Using --arg prevents your data from being incorrectly parsed as syntax. Usually, a shell variable you're passing into jq contains literal data, so this is the correct thing.

In this case, your variable contains syntax, not literal data: The . isn't part of the string you want to do a lookup by, but is instead an instruction to jq to do two separate lookups one after the other.

So, in this case, you should do the more obvious thing, instead of using --arg :

jq -e ".$KEY"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM