Solved

use shell script to parse json returned by a restful service

Posted on 2016-09-08
6
113 Views
Last Modified: 2016-09-13
I have a Restful web service with a URL like "http://myhost:5000/getname" to return a JSON string like this:
{"firstName": "john", "lastName": "doe"}

Open in new window

.

Now that I need to use Linux shell to call the web service and parse the returned JSON string to get the first name and last name. They need to be stored into two variebles and printed out.

Please use out-of-box shell scripting. Don't use any utilities that need to be installed separately.

I am new to shell scripting, so would some one please kindly share your thoughts? Thanks!
0
Comment
Question by:condor888
  • 3
  • 2
6 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 250 total points
ID: 41789232
Assuming that "awk" is sufficiently "out of the box":

STR='{"firstName": "john", "lastName": "doe"}'
set $(awk '{for(n=1;n<=NF;n++)
           {if($n~"first") F=$(n+1);
            if($n~"last") L=$(n+1)}}
          END {gsub("[\",){}]","",F);
           gsub("[\",){}]","",L);
           print F,L}' <<<"$STR")
FIRST=$1
LAST=$2
echo $FIRST
echo $LAST

Open in new window

All we do here is parsing the string field for field for the occurrence of "first" or "last". If found, we assume the following field to be the value for firstname or lastname, respectively.
We strip the remaining special characters from the values and print them.
$() denotes "command substitution". It will be replaced by the result of the command between the parentheses.
"set" assigns the variables $1, $2, etc to the space-delimited values of the given string.
We create new variables from these $1, $2 variables to get meaningful names and print them out.

STR='... ...' is just a placeholder because I don't know your way of calling the web service.
Replace it with something like
STR=$(command to call the web service returning the string)

If your shell is not bash we'll have to do a minor modification. Please let me know if this is the case.
0
 

Author Comment

by:condor888
ID: 41789236
Thanks, woolmilkporc. It works. However, I don't understand some of the details. So is there a good resource that I can resort to in order to understand more about shell scripting? Thanks!
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 41789249
Here is a rather good bash guide/tutorial:
http://www.bash.academy/
A comprehensive list of tutorials available on the web:
http://wiki.bash-hackers.org/scripting/tutoriallist
Finally, this is the complete bash Reference Manual:
https://www.gnu.org/software/bash/manual/bash.html

As for "awk", which is kind of a languange on its own:
A tutorial (one of many out there): http://www.grymoire.com/Unix/Awk.html
The user's Guide: https://www.gnu.org/software/gawk/manual/gawk.html

I'm sure you'll find tons of other resources by searching the web.
Have fun and success!

wmp
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:condor888
ID: 41789251
Ok, I will check them out. Thank you again for providing good learning resources.
0
 
LVL 33

Assisted Solution

by:shalomc
shalomc earned 250 total points
ID: 41792596
> "Please use out-of-box shell scripting. Don't use any utilities that need to be installed separately."

This is a silly requirement, looks like a homework assignment.
Shell scripting was not designed to deal with everything and all types of content.
So in your case, I recommend getting jq, dubbed "awk for json", and running this instead of the awk monster
STR='{"firstName": "john", "lastName": "doe"}'
FIRST=`echo $STR | jq -r ".firstName"`
LAST=`echo $STR | jq -r ".lastName"`
echo $FIRST
echo $LAST

Open in new window

0
 

Author Closing Comment

by:condor888
ID: 41797251
Thanks for the solutions. It helped a lot!
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes a user will call me frantically, explaining that something has gone wrong and they have tried everything (read - they have messed it up more and now need someone to clean up) and it still does no good, can I help them?!  Usually the standa…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question