如何使用jq从JSON获取键名 [英] How to get key names from JSON using jq
本文介绍了如何使用jq从JSON获取键名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
curl http://testhost.test.com:8080/application/app/version | jq '.version' | jq '.[]'
以上命令仅输出以下值:
The above command outputs only the values as below:
"madireddy@test.com"
"2323"
"test"
"02-03-2014-13:41"
"application"
如何获取密钥名称,如下所示:
How can I get the key names instead like the below:
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阵列:
UPDATE: To create a BASH array using these keys:
使用BASH 4 +:
Using BASH 4+:
mapfile -t arr < <(jq -r 'keys[]' ms.json)
在较旧的BASH上,您可以执行以下操作:
On older BASH you can do:
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"
这篇关于如何使用jq从JSON获取键名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文