Solved

use shell script to parse json returned by a restful service

Posted on 2016-09-08
6
122 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 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
Report: Liquid Web beats Amazon, Rackspace & More

A study by performance analyst firm Cloud Spectator finds that Liquid Web beats rivals Amazon, Rackspace and DigitalOcean when it comes to website and cloud application performance.

 

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Windows 7 does not have the best desktop search built in. This is something Windows 7 users have struggled with. You type something in, and your search results don’t always match what you are looking for, or it doesn’t actually work at all. There ar…
In order to have all security and back ups taken care of, WordPress users can sign up for services with WP Engine.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

738 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