Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

use shell script to parse json returned by a restful service

Posted on 2016-09-08
6
Medium Priority
?
172 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 1000 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
Threat Trends for MSPs to Watch

See the findings.
Despite its humble beginnings, phishing has come a long way since those first crudely constructed emails. Today, phishing sites can appear and disappear in the length of a coffee break, and it takes more than a little know-how to keep your clients secure.

 

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 1000 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

2017 Webroot Threat Report

MSPs: Get the facts you need to protect your clients.
The 2017 Webroot Threat Report provides a uniquely insightful global view into the analysis and discoveries made by the Webroot® Threat Intelligence Platform to provide insights on key trends and risks as seen by our users.

Question has a verified solution.

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

The purpose of this article is to demonstrate how we can use conditional statements using Python.
I use more than 1 computer in my office for various reasons. Multiple keyboards and mice take up more than just extra space, they make working a little more complicated. Using one mouse and keyboard for all of my computers makes life easier. This co…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

886 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