5

i need to print key and values from a json string. i allready parse a simple json string

            {
              "Name": "test1",
              "CreateDate": "2016-08-30T10:52:52Z",
              "Id": "testId1",
            }

my code like this

 q1=$(echo $x | grep -Po '"Name":.*?[^\\]",'| perl -pe 's/"Name": //; s/^"//; s/",$//');

 q2=$(echo $x | grep -Po '"Id":.*?[^\\]",'| perl -pe 's/"Id": //; s/^"//; s/",$//');

    echo $q1 "," $q2;

But this code is not applicable for json string like this

x='{    "TestNames":
        [{
        "Name": "test1",
        "CreateDate": "2016-08-30T10:52:52Z",
        "Id": "testId1"
         }, 
         {
        "Name":  "test2",
        "CreateDate": "2016-08-30T10:52:13Z",
        "Id": "testId2"
    }]
}';

I need to print like this

test1 , testId1
test2 , testId2

is it possible to get data like this using grep command?

8
  • the data is not valid json. A json parser would say: parse error: Expected another key-value pair at line 6, column 10 (The command after a key-value pair is only allowed if it is followed by another key/value pair) Commented Aug 30, 2016 at 12:21
  • Since everyone suggests you to install 3rd party programs.. If your JSON Format does not change and your key's are limited and you need a pure Bash+grep solution you could grep the Names and Ids in 2 bash Arrays separately and then print them side by side. Commented Aug 30, 2016 at 12:30
  • @makadev If your "JSON" Format does not change and your key's are limited and you need a pure Bash+grep solution ... you are doing something wrong. Commented Aug 30, 2016 at 12:31
  • @hek2mgl why, because of giving another solution? You do notice that your "duplicates" accepted answer suggest installation of a js/json parser, so do many other.. this may not be an option in certain environments. If it was a good solution, I wouldn't write it in comments. Besides, no one noticed the usage of perl.. why not use perls JSON module... Commented Aug 30, 2016 at 12:36
  • If your application requires to process json and you have nothing that can parse json on your servers plus you can't install something then you are doing it wrong. Commented Aug 30, 2016 at 12:38

2 Answers 2

11

First, your data is not valid json, there is a comma too much:

{
  "TestNames": [
    {
      "Name": "test1",
      "CreateDate": "2016-08-30T10:52:52Z",
      "Id": "testId1", <--- Remove that!
    },
    {
      "Name": "test2",
      "CreateDate": "2016-08-30T10:52:13Z",
      "Id": "testId2"
    }
  ]
}

Once you've fixed that you can use jq for parsing json on the command line:

echo "$x" | jq -r '.TestNames[]|"\(.Name) , \(.Id)"'

if you need to keep the output values.

declare -A map1

while read name id ; do
    echo "$name"
    echo "$id"
    map1[$name]=$id

done < <(echo "$x" | jq -r '.TestNames[]|"\(.Name) \(.Id)"')

echo "count : ${#map1[@]}"
echo "in loop: ${map1[$name]}"
Sign up to request clarification or add additional context in comments.

8 Comments

is it possible to add result to variables separatly, using this method? (eg : print1= echo "$x" | jq -r '.TestNames[]|"(.Name) , (.Id)"'; echo $print1;
Are you searching for command substitution?
i need to create a hash table(key:name,value=Id) using json parse output. because i need Name and Id separately. is it possible with this solution?
I would use a while loop, like this: pastebin.com/SEh22pAH
thanks hek2mgl, its working for me. i look solution like this
|
2

I'd recommend using jq, a command-line JSON parser :

$ echo '''{
          "Name": "test1",
          "CreateDate": "2016-08-30T10:52:52Z",
          "Id": "testId1"
        }''' | jq  '.Name + " , " + .Id'

"test1 , testId1"


$ echo '''{    "TestNames":
    [{
    "Name": "test1",
    "CreateDate": "2016-08-30T10:52:52Z",
    "Id": "testId1"
     },
     {
    "Name":  "test2",
    "CreateDate": "2016-08-30T10:52:13Z",
    "Id": "testId2"
}]
}''' | jq '.TestNames[] | .Name + " , " + .Id'

"test1 , testId1"
"test2 , testId2"

3 Comments

.TestNames | .[] should be simply TestNames[].
Thanks, I had tried .TestNames.[] at first and found it strange it wasn't working.
Careful! .TestNames[], not .TestNames.[]

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.