Read JSON Multiple Values into Bash Variable - not able to use any 3rd party tools like jq etc
<p>This has been asked a million times and I know there are a million solns. However im restricted in that I cant install anything on this client server , so I have whatever bash can come up with :)</p>

<p>I'm referencing <a href="http://stackoverflow.com/questions/1955505/parsing-json-with-unix-tools">Parsing JSON with UNIX tools</a> and using this to read data and split into lines. </p>

<pre><code>$ cat demo.json
{"rows":[{"name":"server1.domain.com","Access":"Owner","version":"99","Business":"Owner1","Owner2":"Main_Apprve","Owner1":"","Owner2":"","BUS":"Marketing","type":"data","Egroup":["ALPHA","BETA","GAMA","DELTA"],"Ename":["D","U","G","T","V"],"stage":"TEST"}]}
</code></pre>

<p>However as you can see it splits the "Egroup" and others with multiple entries into single lines making it a little bit more difficult.</p>

<pre><code>cat demo.json |     sed -e 's/[{}]/''/g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i&lt;=n; i++) print a[i]}&gt;
    "rows":["name":"server1.domain.com"
    "Access":"Owner"
    "version":"99"
    "Business":"Owner1"
    "Owner2":"Main_Apprve"
    "Owner1":""
    "Owner2":""
    "BUS":"Marketing"
    "type":"data"
    "Egroup":["ALPHA"
    "BETA"
    "GAMA"
    "DELTA"]
    "Ename":["D"
    "U"
    "G"
    "T"
    "V"]
    "stage":"TEST"]
</code></pre>

<p>Im trying to capture the data so i can list using a shell script. How would you advise me to capture each variable and then reuse in reporting in a shell script?</p>

<pre><code> grep -Po '"Egroup":.*?[^\\]",' demo.json
"Egroup":["ALPHA",
</code></pre>

<p>As you can see this wouldn't work for lines with more than 1 entry.</p>

<p>Thoughts appreciated. ( btw Im open to python and perl options but without having to install any extra modules to use with json ) </p>

<p>It's simple using <code>Python</code>.</p>

<p><strong>Example</strong></p>

<pre><code>$ python -c 'import sys, json; print json.load(sys.stdin)["rows"][0]["Egroup"]' &lt;demo.json 
[u'ALPHA', u'BETA', u'GAMA', u'DELTA']
</code></pre>

python json bash perl shell