Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse json array in shell script [duplicate]

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?

like image 923
Abdul Manaf Avatar asked Aug 30 '16 12:08

Abdul Manaf


2 Answers

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]}"
like image 66
hek2mgl Avatar answered Sep 30 '22 04:09

hek2mgl


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"
like image 27
Aaron Avatar answered Sep 30 '22 05:09

Aaron