8

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.

5 Answers 5

9

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]"}]}
Sign up to request clarification or add additional context in comments.

Comments

7

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

Comments

3

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]

Comments

1

Using standard unix toolbox : sed command

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

1 Comment

unfortunately this will not work with complex objects
-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.