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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23118341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:16:51  来源:igfitidea点击:

How to get key names from JSON using jq

jsonshellunixkeyjq

提问by Ezhilan Mahalingam

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

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

The above command outputs only the values as below:

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

"[email protected]"

"2323"

"test"

"02-03-2014-13:41"

"application"

How can I get the key names instead like the below:

我怎样才能得到键名,而不是像下面这样:

email

versionID

context

date

versionName

回答by anubhava

You can use:

您可以使用:

$ 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"
]

UPDATE:To create a BASH array using these keys:

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

Using BASH 4+:

使用 BASH 4+:

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

On older BASH you can do:

在较旧的 BASH 上,您可以执行以下操作:

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

Then print it:

然后打印出来:

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"

回答by Chris Stryczynski

You need to use jq 'keys[]'. For example:

您需要使用jq 'keys[]'. 例如:

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

Will output a line separated list:

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

"example1"
"example2"
"example3"

回答by Elliot Pahl

In combination with the above answer, you want to ask jq for raw output, so your last filter should be eg.:

结合上面的答案,您想向 jq 询问原始输出,因此您的最后一个过滤器应该是例如:

     cat input.json | jq -r 'keys'

From jq help:

来自 jq 帮助:

     -r     output raw strings, not JSON texts;

回答by nrb

To print keys on one line as csv:

要将密钥在一行上打印为 csv:

echo '{"b":"2","a":"1"}' | jq -r 'keys | [ .[] | tostring ] | @csv'

Output:

输出:

"a","b"

For csv completeness ... to print values on one line as csv:

为了 csv 完整性......在一行上打印值作为 csv:

echo '{"b":"2","a":"1"}' | jq -rS . | jq -r '. | [ .[] | tostring ] | @csv'

Output:

输出:

"1","2"

回答by hrushikesh

echo '{"ab": 1, "cd": 2}' | jq -r 'keys[]'prints all keys one key per line without quotes.

echo '{"ab": 1, "cd": 2}' | jq -r 'keys[]'每行打印所有键一个键,不带引号。

ab
cd

回答by Ron Martinez

Here's another way of getting a Bash array with the example JSON given by @anubhava in his answer:

这是使用@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'

回答by Gianfranco P.

To get the keys on a deeper node in an JSON:

要在 JSON 中获取更深节点上的键:

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