Curl http://testhost.test.com:8080/application/app/version | jq '。版本' | jq '.[]'

上面的命令只输出如下值:

"madireddy@test.com"

"2323"

"test"

"02-03-2014-13:41"

"application"

我怎样才能得到像下面这样的键名:

email

versionID

context

date

versionName

当前回答

你需要使用jq 'keys[]'。例如:

echo '{"example1" : 1, "example2" : 2, "example3" : 3}' | jq 'keys[]'

将输出一个行分隔的列表:

"example1"
"example2"
"example3"

其他回答

如果你的输入是一个对象数组,

[
  { 
    "a01" : { "name" : "A", "user" : "B" }
  },
  { 
    "a02" : { "name" : "C", "user" : "D" }
  }
]

试一试:

jq '.[] | keys[]'

下面是@anubhava在他的回答中给出的JSON示例获得Bash数组的另一种方式:

arr=($(jq --raw-output 'keys_unsorted | @sh' file.json))

echo ${arr[0]}    # 'Archiver-Version'
echo ${arr[1]}    # 'Build-Id'
echo ${arr[2]}    # 'Build-Jdk'

你需要使用jq 'keys[]'。例如:

echo '{"example1" : 1, "example2" : 2, "example3" : 3}' | jq 'keys[]'

将输出一个行分隔的列表:

"example1"
"example2"
"example3"

获取JSON中更深层次节点上的键:

echo '{"data": "1", "user": { "name": 2, "phone": 3 } }' | jq '.user | keys[]'
"name"
"phone"

要按键在原始JSON中出现的顺序获取键,请使用:

jq 'keys_unsorted' file.json

如果你想要键按字母数字排序,你可以使用:

jq 'keys' file.json

完整的示例

$ cat file.json
{ "Created-By" : "Apache Maven", "Build-Number" : "", "Archiver-Version" : "Plexus Archiver", "Build-Id" : "",  "Build-Tag" : "", "Built-By" : "cporter"}

$ jq 'keys_unsorted' file.json                                         
[
  "Created-By",
  "Build-Number",
  "Archiver-Version",
  "Build-Id",
  "Build-Tag",
  "Built-By"
]

$ jq 'keys' file.json
[
  "Archiver-Version",
  "Build-Id",
  "Build-Number",
  "Build-Tag",
  "Built-By",
  "Created-By"
]