Gripsiden Gripsiden - 1 year ago 65
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

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

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

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 Source

Perl 1-liner using JSON module:

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



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/JSON/   # <- put it here

perl -lane -I/working 'use JSON::PP; my $data = decode_json($_); print join( ",", @{ $data->{rows}->[0]->{Egroup} } )' demo.json   
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download