如何在bash脚本中迭代json

时间:2021-07-15 15:27:05

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":"asa@xyz.com"},{"username":"qq","name":"qq Morris","mail":"qq@xyz.com"},{"username":"qwe","name":"qwe Org","mail":"qwe@xyz.com"}]}

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

Output can be as

输出可以是

mail=asa@xyz.com,qq@xyz.com,qwe@xyz.com

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.

我仍然无法获得具体的输出。

5 个解决方案

#1


7  

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

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

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

#2


3  

You should use json_pp tool (in debian, it is part of the libjson-pp-perl package)

你应该使用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/.* : "\(.*\)".*/\1/'`
for LINE in $LINES ; do
    MAILS="$LINE,$MAILS"
done
echo $MAILS | sed 's/.$//'

Output :

qwe@xyz.com,qq@xyz.com,asa@xyz.com

#3


1  

Using standard unix toolbox : sed command

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

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

#4


0  

With R you could do this as follows:

使用R,您可以执行以下操作:

$ value={"count":5,"users":[{"username":"asa","name":"asa Tran","mail":"asa@xyz.com"},{"username":"qq","name":"qq Morris","mail":"qq@xyz.com"},{"username":"qwe","name":"qwe Org","mail":"qwe@xyz.com"}]}
$ echo $value | R path users | R map path mail
["asa@xyz.com", "qq@xyz.com", "qwe@gyz.com"]

#5


0  

jq is 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:

asa@xyz.com
qq@xyz.com
qwe@xyz.com

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":"asa@xyz.com"},{"username":"qq","name":"qq Morris","mail":"qq@xyz.com"},{"username":"qwe","name":"qwe Org","mail":"qwe@xyz.com"}]}

#1


7  

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

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

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

#2


3  

You should use json_pp tool (in debian, it is part of the libjson-pp-perl package)

你应该使用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/.* : "\(.*\)".*/\1/'`
for LINE in $LINES ; do
    MAILS="$LINE,$MAILS"
done
echo $MAILS | sed 's/.$//'

Output :

qwe@xyz.com,qq@xyz.com,asa@xyz.com

#3


1  

Using standard unix toolbox : sed command

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

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

#4


0  

With R you could do this as follows:

使用R,您可以执行以下操作:

$ value={"count":5,"users":[{"username":"asa","name":"asa Tran","mail":"asa@xyz.com"},{"username":"qq","name":"qq Morris","mail":"qq@xyz.com"},{"username":"qwe","name":"qwe Org","mail":"qwe@xyz.com"}]}
$ echo $value | R path users | R map path mail
["asa@xyz.com", "qq@xyz.com", "qwe@gyz.com"]

#5


0  

jq is 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:

asa@xyz.com
qq@xyz.com
qwe@xyz.com

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":"asa@xyz.com"},{"username":"qq","name":"qq Morris","mail":"qq@xyz.com"},{"username":"qwe","name":"qwe Org","mail":"qwe@xyz.com"}]}