0

In one of my projects exist json file

{
  "name": "view1",
  "version": "1.1.1",
  "target-dir": "/opt/project/spark-jobs/",
  "target-hosts": {
    "dev": ["host1", "host2"],
    "prod": ["host3", "host4"]
  }
}

I have to read element of json "target-hosts", for solve try to use JQ, but if try run command

jq '.target-hosts' deploy.json  
jq: error: dir/0 is not defined at <top-level>, line 1:
.target-hosts        
jq: 1 compile error

How escape character "-"?

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
Nikolay Baranenko
  • 1,582
  • 6
  • 35
  • 60

3 Answers3

1
."target-hosts"

or

.["target-hosts"]

So,

jq '."target-hosts"' deploy.json

or

jq '.["target-hosts"]' deploy.json
ikegami
  • 367,544
  • 15
  • 269
  • 518
0

Just need to quote the key name such as

jq '."target-hosts"' deploy.json  

Demo

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
0

You need to enclose in double quotes:

jq '."target-hosts"'
nurçin
  • 54
  • 6