json - 如何使用 jq 从 JSON 中获取键名

curl http://testhost.test.com:8080/application/app/version | jq '.version' | jq '.[]'

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

"madireddy@test.com"

"2323"

"test"

"02-03-2014-13:41"

"application"

如何获取键名,如下所示:

email

versionID

context

date

versionName

最佳答案

你可以使用:

jq 'keys' file.json

完整示例

$ cat file.json
{ "Archiver-Version" : "Plexus Archiver", "Build-Id" : "", "Build-Jdk" : "1.7.0_07", "Build-Number" : "", "Build-Tag" : "", "Built-By" : "cporter", "Created-By" : "Apache Maven", "Implementation-Title" : "northstar", "Implementation-Vendor-Id" : "com.test.testPack", "Implementation-Version" : "testBox", "Manifest-Version" : "1.0", "appname" : "testApp", "build-date" : "02-03-2014-13:41", "version" : "testBox" }

$ jq 'keys' file.json
[
  "Archiver-Version",
  "Build-Id",
  "Build-Jdk",
  "Build-Number",
  "Build-Tag",
  "Built-By",
  "Created-By",
  "Implementation-Title",
  "Implementation-Vendor-Id",
  "Implementation-Version",
  "Manifest-Version",
  "appname",
  "build-date",
  "version"
]

更新:使用这些键创建 BASH 数组:

使用 BASH 4+:

mapfile -t arr < <(jq -r 'keys[]' ms.json)

在较旧的 BASH 上,您可以这样做:

arr=()
while IFS='' read -r line; do
   arr+=("$line")
done < <(jq 'keys[]' ms.json)

然后打印出来:

printf "%s\n" ${arr[@]}

"Archiver-Version"
"Build-Id"
"Build-Jdk"
"Build-Number"
"Build-Tag"
"Built-By"
"Created-By"
"Implementation-Title"
"Implementation-Vendor-Id"
"Implementation-Version"
"Manifest-Version"
"appname"
"build-date"
"version"

https://stackoverflow.com/questions/23118341/

相关文章:

json - 部分 JSON 在 Go 中解码为 map

json - react-native 如何从本地JSON文件中获取数据?

c# - XmlDocument - 从字符串加载?

javascript - 以 JSON 格式发布数据

c# - DataContractJsonSerializer 和 JavaScriptSerial

asp.net-mvc - 从 Asp.net WEBAPI 显式返回 JSON 字符串?

javascript - ES6 模块实现,如何加载 json 文件

json - 如何使用 curl 将 json 对象与数组一起放置

json - 将 JSON 传递给 HTTP POST 请求

jquery - 为什么找不到我的 json 文件?