How to convert the output of a command to JSON format

If you are coding or doing command line and need to grab the output of a command line tool or XML and co type files and export all that to JSON or dictionaries for Python, j got what you need.

It’s called JC and it’s available as a command line tool or a Python lib. You can thus redirect with a pipe the output of a command to JC like this by specifying the dig parser (There are plenty of parsers available):

dig | jc --dig

And so get this:

"when":"Fri Apr 16 16:09:00 PDT 2021","rcvd":56,"when_epoch":1618614540,

If you feel like growing wings, you can even chain it with the jq command like this to simply retrieve information:

dig | jc --dig | jq -r '. []. answer []. data'

Very practical for retrieving data in your Python scripts or outputting this data in JSON and then importing it elsewhere. With this you can parse commands like crontab, ls, free, mount, hosts, ping, ps, systemctl, tracerout or file formats like CSV, XML, YAML…etc.

There’s even an online demo if you want to experiment with installing .

If you’re interested, the Github repository is here.

If you liked this article, please subscribe to our reddit community to discuss it. You can also find us on Twitter and Facebook.

5/5 - (2 votes)
See also  Bat – A cat clone with wings

Newsletter Updates

Enter your email address below to subscribe to our newsletter

Leave a Reply