0

I have json string like this:

jstring='[{"userQuery":"select name from abc;","user":"abc"},{"userQuery":"select name from xyz;","user":"xyz"},{"userQuery":"select name from ppp;","user":"ppp"}]'

I wrote a simple for loop using jq to extract values but not getting the desired result.

for i in `echo $jstring | jq '.[] | [.user, .userQuery]'`; do echo ${i}; done

With the help of this line : echo $jstring | jq '.[] | [.user, .userQuery]'. I am able to extract below info:

[ "abc", "select name from abc;"][ "xyz", "select name from xyz;"][ "ppp", "select name from ppp;"]

Now, I want two variable "user" & "query" for each array and store that info.
Eg: For [ "abc", "select name from abc;"] -- user: abc, query: "select name from abc" and store them.

I am not sure how to iterate over json using jq and get individual values and store them.

4

2 Answers 2

3

I assume you mean you want to store them as shell variables. With bash you can write:

while read -r user query; do 
    echo "user->$user"
    echo "query->$query"
done < <( jq -r '.[] | "\(.user) \(.userQuery)"' <<< "$jstring" )
user->abc
query->select name from abc;
user->xyz
query->select name from xyz;
user->ppp
query->select name from ppp;

The secret sauce is in the formulation of the string inside jq that refers to the object properties with \(.user)

I'm assuming that the user name does not contain spaces. Otherwise we have to use a different separator in that string, and use IFS with the shell read command.

Sign up to request clarification or add additional context in comments.

1 Comment

@gelnnjackman Thank you for the quick response and answer. This is perfect! I was looking at this as well: stackoverflow.com/questions/46187807/… but couldn't make it work. Thanks again!
2

jq + bash solution:

#!/bin/bash
jstring='[{"userQuery":"select name from abc;","user":"abc"},{"userQuery":"select name from xyz;","user":"xyz"},{"userQuery":"select name from ppp;","user":"ppp"}]'

while IFS=$'\t' read -r user query; do
    msg="User: ${user} is running : ${query}"
    mail -s "User query" "[email protected]" <<< "$msg"
done < <(jq '.[] | [.user,.userQuery] | @tsv' <<< "$jstring")

3 Comments

The above code did not work as expected, the code which Glenn has posted is working as expected. Please check the output, it is not working as expected.
@PanwarS87, at first, it works fine in terms of parsing and processing. Secondly, your expected output(as you said) was not clear. It was I want two variable "user" & "query" for each array and store that info - which does not look/sound clear. So I've done the man and crucial work. So, your claim sounds as preconceived
Sorry, I might sound unclear but when I ran the above code I am getting the below output. User: "abc\tselect name from abc;" is running : User: "xyz\tselect name from xyz;" is running : User: "ppp\tselect name from ppp;" is running : I may be missing something here or may be IFS is not working as expected.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.