bash 如何在bash脚本中遍历json

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19488408/
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-18 08:21:41  来源:igfitidea点击:

How to iterate through json in bash script

jsonbash

提问by Sarang

I have the json as below, i need to get only the mail from the above json in bash script

我有如下的 json,我只需要在 bash 脚本中从上面的 json 中获取邮件

value={"count":5,"users":[{"username":"asa","name":"asa Tran","mail":"[email protected]"},{"username":"qq","name":"qq Morris","mail":"[email protected]"},{"username":"qwe","name":"qwe Org","mail":"[email protected]"}]}

value={"count":5,"users":[{"username":"asa","name":"asa Tran","mail":"[email protected]"},{"username": "qq","name":"qq Morris","mail":"[email protected]"},{"username":"qwe","name":"qwe Org","mail":"qwe @xyz.com"}]}

Output can be as

输出可以是

[email protected],[email protected],[email protected]

All the above need to be done in the bash script (.sh)

以上所有都需要在bash脚本(.sh)中完成

I have already tried with the array iteration as but of no use

我已经尝试过数组迭代,但没有用

for key in "${!value[@]}"
do
        #echo "key = $key"
        echo "value = ${value[$key]}"
done

Even i have tried with the array conversion as

即使我已经尝试过将数组转换为

alias json-decode="php -r 'print_r(json_decode(file_get_contents(\"php://stdin\"),1));'" value=$(curl --user $credentials -k $endPoint | json-decode)

别名 json-decode="php -r 'print_r(json_decode(file_get_contents(\"php://stdin\"),1));'" value=$(curl --user $credentials -k $endPoint | json-解码)

Still i was not able to get the specific output.

我仍然无法获得特定的输出。

采纳答案by cbliard

If this is valid json and the email field is the only one containing a @character, you can do something like this:

如果这是有效的 json 并且 email 字段是唯一包含@字符的字段,则可以执行以下操作:

echo $value | tr '"' '\n' | grep @

It replaces double-quotes by new line character and only keeps lines containing @. It is really not json parsing, but it works.

它用换行符替换双引号并且只保留包含@. 它真的不是 json 解析,但它有效。

You can store the result in a bash array

您可以将结果存储在 bash 数组中

emails=($(echo $value | tr '"' '\n' | grep @))

and iterate on them

并迭代它们

for email in ${emails[@]}
do
    echo $email
done

回答by Thibault

You should use json_pptool (in debian, it is part of the libjson-pp-perlpackage)

您应该使用json_pp工具(在 debian 中,它是libjson-pp-perl软件包的一部分)

One would use it like this :

人们会像这样使用它:

cat file.json | json_pp

cat file.json | json_pp

And get a pretty print for your json.

并为您的 json 打印漂亮的照片。

So in your case, you could do :

所以在你的情况下,你可以这样做:

#!/bin/bash
MAILS=""  
LINES=`cat test.json | json_pp | grep '"mail"' | sed 's/.* : "\(.*\)".*//'`
for LINE in $LINES ; do
    MAILS="$LINE,$MAILS"
done
echo $MAILS | sed 's/.$//'

Output :

输出 :

[email protected],[email protected],[email protected]

回答by Chris Maes

jqis the tool to iterate through a json. In your case:

jq是遍历 json 的工具。在你的情况下:

while read user; do
    jq -r '.mail' <<< $user
done <<< $(jq -c '.users[]' users.json)

would give:

会给:

[email protected]
[email protected]
[email protected]

NOTE: I removed "value=" because that is not valid json. Users.json contains:

注意:我删除了“value=”,因为那不是有效的 json。Users.json 包含:

{"count":5,"users":[{"username":"asa","name":"asa Tran","mail":"[email protected]"},{"username":"qq","name":"qq Morris","mail":"[email protected]"},{"username":"qwe","name":"qwe Org","mail":"[email protected]"}]}

回答by Yann Moisan

Using standard unix toolbox : sedcommand

使用标准的 unix 工具箱:sed命令

cat so.json | sed "s/},/\n/g" | sed 's/.*"mail":"\([^"]*\)".*//'

回答by Jacek Pospychala

With Ryou could do this as follows:

使用R,您可以按如下方式执行此操作:

$ value={"count":5,"users":[{"username":"asa","name":"asa Tran","mail":"[email protected]"},{"username":"qq","name":"qq Morris","mail":"[email protected]"},{"username":"qwe","name":"qwe Org","mail":"[email protected]"}]}
$ echo $value | R path users | R map path mail
["[email protected]", "[email protected]", "[email protected]"]