Pass bash variable (string) as jq argument to walk JSON object -


this seemingly very basic question. new json, prepared incoming facepalm.

i have following json file (test-app-1.json):

"application.name": "test-app-1", "environments": {     "development": [         "server1"     ],     "stage": [         "server2",         "server3"     ],     "production": [         "server4",         "server5"     ] } 

the intent use configuration file , reference input validation.

i using bash 3.2 , using jq 1.4 (not latest) read json.

the problem: need return values in specified json array based on argument.

example: (how documentation , other resources show should work)

application_environment="developement"

jq --arg appenv "$application_environment" '.environments."$env[]"' test-app-1.json

if executed, returns null. should return server1.

obviously, if specify text explicitly matching json arrays under environment, works fine: jq 'environments.development[]' test-app-1.json returns: server1.

limitations: stuck jq 1.4 project. have tried same actions in 1.5 on different machine same null results.

what doing wrong here?

jq documentation: https://stedolan.github.io/jq/manual/

you have 3 issues - 2 typos , 1 jq filter usage issue:

  • set application_environment development instead of developement
  • use variable name consistently: if define appenv, use $appenv, not $env
  • address .environments[$appenv]

when fixed, looks this:

$ application_environment="development" $ jq --arg appenv "$application_environment" '.environments[$appenv][]' test-app-1.json "server1" 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -