skip to Main Content

Executing the following jq command works fine on Mac OS Catalina (jq version 1.6) :

echo $(jq '.paths | to_entries | map(select(.value[].tags | index("Tag123"))) | from_entries' custom.json)

However executing it on CentOS (CentOS release 6.9 (Final)) (jq version 1.3) returns the following error :

echo $(jq '.paths | to_entries | map(select(.value[].tags | index("Shell"))) | from_entries' custom.json)
error: index is not defined
.paths | to_entries | map(select(.value[].tags | index("Shell"))) | from_entries
                                                 ^^^^^
1 compile error

2

Answers


  1. Evidently the version of jq installed via pip was version 1.3 (which is ancient history) or earlier. index/1 was only introduced as a built-in filter after the release of jq 1.3.

    If you are absolutely stuck with jq 1.3, you could use contains/1 if you don’t need the integer index.

    Login or Signup to reply.
  2. Note that pip install jq does not install the executable that you can use interactively from the command line, but as a python package, which you can use only in scripts.

    As for the disparity in the usage of index() function, you seem to be using an older/outdated version of jq-1.3 in CentOS which does not have the index(), rindex() functions.

    From the changelog

    New features in 1.4 since 1.3:

    • string built-ins:
    • index, rindex, indices

    Your version on MacOS jq-1.6 has those functions available.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search