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 example.com | jc --dig

And so get this:

[{"id":38052,"opcode":"QUERY","status":"NOERROR","flags":["qr","rd","ra"],
"query_num":1,"answer_num":1,"authority_num":0,"additional_num":1,
"opt_pseudosection":{"edns":{"version":0,"flags":[],"udp":4096}},"question":
{"name":"example.com.","class":"IN","type":"A"},"answer":[{"name":
"example.com.","class":"IN","type":"A","ttl":39049,"data":"93.184.216.34"}],
"query_time":49,"server":"2600:1700:bab0:d40::1#53(2600:1700:bab0:d40::1)",
"when":"Fri Apr 16 16:09:00 PDT 2021","rcvd":56,"when_epoch":1618614540,
"when_epoch_utc":null}]

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

dig example.com | 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