Tuesday, July 24, 2012

JSAWK

Last week I got a mail from my lead "Hey !! we need all the sql queries that are executed when we make any api call", I was like, really???That much of copy paste I'll have to do(poor me). Anyway I had all the rest calls and the queries for them in JSON format, all I had to do was to write one awk script. But with awk too I knew I will have to do lots of formatting so I searched online if there is any command line JSON parser or any utility so that i don't have to do it manually and then I found one cool tool "JSAWK"(AWK with javascript power).

Its pretty cool and if you know awk its pretty easy to use. I am not an expert, I am just sharing what I learnt and what I did.

Lets take a simple JSON data example :
{ "A" :[{ "B":"b","C":"c"},{"D":"d","E":"e"}]}

above is one simple json, now i want to extract each key value pair, here is a valid jsawk command.
Supposing above json is in file json.txt

cat json.txt | jsawk 'return this.A'
response := [{"B":"b","C":"c"},{"D":"d","E":"e"}]

Here we are getting an array as response what if I want to get elements of array??Let's try.


cat json.txt | jsawk 'return this.A[0]'
response := {"B":"b","C":"c"}


You can even use |(pipes) with JSAWK
cat json.txt | jsawk 'return this.A[0]' | jsawk 'return this.B'
response := b



What JSAWK does is it uses javascript to filter JSON arrays in results array and print them to stdout.
Since it gives you an array as result, traversing and fetching data becomes too easy. These are ways to access the elements now what if we want to modify our json, make some selection on elements?? Can we do that??


YES!!!!We have couple of options with jsawk to help us, they are below :


Options                              Usage
-n                                        Suppress the printing of result set


-q                                        Filter Json through JSONQuery query


-v <name=value>                Set global value name to value


-f                                          load and run specified javascript file before processing the JSON


-a <script>                           Run the specified snippet of javascript after processing JSON input.


-b<script>                            Run the specified snippet of javascript before processing JSON input.


JSAWK supports spidermonkey Javascript interpreter and hence all the powers of it plus it has some other methods available. For more info check this link 


https://github.com/micha/jsawk

Next time you have to parse some JSON, you know what you can use.

2 comments:

  1. Nice one again.. but can we implement this in any automated environment?What I mean is you won't be executing the same on command prompt every time your lead says, so better to have it included as a library or some analogy and make it store the results in some log file.

    ReplyDelete
  2. I wrote a shell script for myself using it. You can write any library but it totally depends on what use you have so coming up with a generic library is tough.

    ReplyDelete