Solved

use shell script to parse json returned by a restful service

Posted on 2016-09-08
6
77 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:condor888
Comment Utility
Ok, I will check them out. Thank you again for providing good learning resources.
0
 
LVL 32

Assisted Solution

by:shalomc
shalomc earned 250 total points
Comment Utility
> "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
Comment Utility
Thanks for the solutions. It helped a lot!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Windows 10 is here and for most admins this means frustration and challenges getting that first working Windows 10 image. As in my previous sysprep articles, I've put together a simple help guide to get you through this process. The aim is to achiev…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now