Sarang
Sarang

Reputation: 784

How to iterate through json in bash script

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

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

Output can be as

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

All the above need to be done in the bash script (.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)

Still i was not able to get the specific output.

Upvotes: 8

Views: 27466

Answers (5)

Chris Maes
Chris Maes

Reputation: 37712

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

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:

{"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]"}]}

Upvotes: 9

Jacek Pospychala
Jacek Pospychala

Reputation: 97

With R you could do this as follows:

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

Upvotes: -1

Yann Moisan
Yann Moisan

Reputation: 8281

Using standard unix toolbox : sed command

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

Upvotes: 1

Thibault
Thibault

Reputation: 1596

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

One would use it like this :

cat file.json | json_pp

And get a pretty print for your 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 :

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

Upvotes: 3

cbliard
cbliard

Reputation: 7262

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

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.

You can store the result in a bash array

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

and iterate on them

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

Upvotes: 7

Related Questions