Gripsiden Gripsiden - 1 month ago 7
Perl Question

Read JSON Multiple Values into Bash Variable - not able to use any 3rd party tools like jq etc

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 :)

I'm referencing Parsing JSON with UNIX tools and using this to read data and split into lines.

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


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

cat demo.json | sed -e 's/[{}]/''/g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}>
"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"]


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?

grep -Po '"Egroup":.*?[^\\]",' demo.json
"Egroup":["ALPHA",


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

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

Answer

Perl 1-liner using JSON module:

perl -lane 'use JSON; my $data = decode_json($_); print join( ",", @{ $data->{rows}->[0]->{Egroup} } )' demo.json

Output

ALPHA,BETA,GAMA,DELTA

If you do not have JSON installed, instead of trying to reinvent a JSON parser, you can copy the source of JSON::PP (PP means Pure Perl) and put it in your working directory:

/working/demo.json
/working/JSON/PP.pm   # <- put it here

perl -lane -I/working 'use JSON::PP; my $data = decode_json($_); print join( ",", @{ $data->{rows}->[0]->{Egroup} } )' demo.json   
Comments