Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

use shell script to parse json returned by a restful service

Posted on 2016-09-08
6
Medium Priority
?
160 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 

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

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!

Question has a verified solution.

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

In order to have all security and back ups taken care of, WordPress users can sign up for services with WP Engine.
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
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…

705 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