1

I have a JSON file with data as below:

{
    "servers":{
        "author-name":{
            "server": "app-dv-app01"
        },
        "authorname2":{
            "server": "app-dv-app02"
        }
       }
    } 

I am trying to access the servername based on the username here from a script with the jq as below:

jq .servers.${AUTHOR}.server

For authorname2 it is working fine and returning server app-dv-app02. But for author-name it is throwing error as below. In general I checked that if the Authorname in JSON file contains a "-" then this problem arises.

+ AUTHOR=author-name
+ cat dev-servers.json
+ jq .servers.author-name.server
jq: error: name/0 is not defined at <top-level>, line 1:
.servers.ppataky-unzer.server                 
jq: 1 compile error

Could someone please help here to rectify the correct syntax of jq call.

Thanks a lot.

user14628641
  • 31
  • 2
  • 5
  • This works jq '.servers."author-name".server' – souser Feb 12 '21 at 16:36
  • I have tried this. this doesn't give me a compilation error. However I am using variable to pass value so inside double-quote it is not considering the variable value: cat ${ServerList} | jq '.servers."${AUTHOR}".server' It prints the value as null : + AUTHOR=author-name + jq .servers."${AUTHOR}".server + cat dev-servers.json echo server: null – user14628641 Feb 12 '21 at 17:12
  • This should work cat file.jq | jq ".servers.\"${AUTHOR}\".server" – souser Feb 12 '21 at 17:26
  • I used this but end up with the same jq compilation error: cat | jq ".servers.\"${AUTHOR}\".server" – user14628641 Feb 12 '21 at 17:53

0 Answers0