I used to ask a fellow developer, what would you do if you had some data represented in a JSON array, and you’d like to do some very quick and simple processing, to find out the information you are interested in?
He said he’d write a Golang program to do that.
While there’s nothing wrong with that approach, I think this method is too slow. It will take you at least 10 minutes to write and test a Golang program, especially considering you will need to define all the struct
s before you can parse the JSON data.
When we are troubleshooting production incidents, time is money. Is there any way you can process arbitrary data within 1 minute?
awk
or jq
may work, but they are a little difficult to learn and their effectiveness is limited (they are not turing-complete), so I don’t think these tools are a good investment. JavaScript, however, is a wonderful tool to do such a task:
- It is weak-typed, so you do not have to waste time declaring class / struct.
- It has a great ecosystem, with lots of open-source libraries that can do a lot of things.
- Most developers are already very familiar with JavaScript.
Meet https://github.com/kevinwang15/njq2. Now you can cat yourdata.json | njq2 'aJavaScriptExpression'
. It’s a really great method, I use it every day in work.
(to be continued)