3

I have a working code for parsing a JSON output using KornShell by treating it as a string of characters. The issue I have is that the vendor keeps changing the position of the field that I am intersted in. I understand in JSON, we can parse it by key-value pairs.

Is there something out there that can do this? I am intersted in a specific field and I would like to use it to run the checks on the status of another RESTAPI call.

My sample json output is like this:

JSONDATA   value : 
{
  "status": "success",
  "job-execution-id": 396805,
  "job-execution-user": "flexapp",
  "job-execution-trigger": "RESTAPI"
} 

I would need the job-execution-id value to monitor this job through the rest of the script.

I am using the following command to parse it:

RUNJOB=$(print ${DATA} |cut -f3 -d':'|cut -f1 -d','| tr -d [:blank:]) >>  ${LOGDIR}/${LOGFILE}

The problem with this is, it is field delimited by :. The field position has been known to be changed by the vendors during releases.

So I am trying to see if I can use a utility out there that would always give me the key-value pair of "job-execution-id": 396805, no matter where it is in the json output.

I started looking at jsawk, and it requires the js interpreter to be installed on our machines which I don't want. Any hint on how to go about finding which RPM that I need to solve it?

I am using RHEL5.5.

Any help is greatly appreciated.

Bjoern Rennhak
  • 6,766
  • 1
  • 16
  • 21
E B
  • 1,073
  • 3
  • 23
  • 36
  • Are you tied to kornshell for some reason or could you use a more advanced programming language? There are plenty of libraries in Python, Java, C++, Perl, etc. that can do this. – Jacinda May 24 '13 at 19:19
  • 1
    There are helpers written in BASH which you could use maybe? https://github.com/dominictarr/JSON.sh Probably you don't want that otherwise you would utilize jsawk ? You could also rely on vanilla awk for this http://stackoverflow.com/questions/3919750/parse-json-with-shell-scripting-at-linux . – Bjoern Rennhak May 24 '13 at 19:25
  • I don't understand the basics of your sample code, `RUNJOB=$(... cmd substitution... ) >> ${LOGFILE}`. Does that really work? What is it meant to do? In a reduced test I constructed, I only get a zero LOGFILE. Good luck. – shellter May 25 '13 at 02:40
  • Also be aware that shell variables can contain a *lot* of data, try `wholeFile=$(< MBfile)`, which on copies of that variable, you can do things with like `keyValue=${wholeFile#job-execution-id:}; keyValue=${keyValue% *}` and many others. You may not need all the other processing to get to the key/values you want. Good luck! – shellter May 25 '13 at 02:45

2 Answers2

0

The ast-open project has libdss (and a dss wrapper) which supposedly could be used with ksh. Documentation is sparse and is limited to a few messages on the ast-user mailing list.

The regression tests for libdss contain some json and xml examples.

I'll try to find more info.

Henk Langeveld
  • 8,088
  • 1
  • 43
  • 57
0

Python is included by default with CentOS so one thing you could do is pass your JSON string to a Python script and use Python's JSON parser. You can then grab the value written out by the script. An example you could modify to meet your needs is below.

Note that by specifying other dictionary keys in the Python script you can get any of the values you need without having to worry about the order changing.

Python script:

#get_job_execution_id.py
# The try/except is because you'll probably have Python 2.4 on CentOS 5.5,
# and the straight "import json" statement won't work unless you have Python 2.6+.
try:
    import json
except:
    import simplejson as json
import sys

json_data = sys.argv[1]
data = json.loads(json_data)
job_execution_id = data['job-execution-id']
sys.stdout.write(str(job_execution_id))

Kornshell script that executes it:

#get_job_execution_id.sh
#!/bin/ksh
JSON_DATA='{"status":"success","job-execution-id":396805,"job-execution-user":"flexapp","job-execution-trigger":"RESTAPI"}'
EXECUTION_ID=`python get_execution_id.py "$JSON_DATA"`
echo $EXECUTION_ID
Jacinda
  • 4,932
  • 3
  • 26
  • 37