DEV Community

Rajesh
Rajesh

Posted on

How to Convert Any CLI Output to JSON in 3 Lines of Code (DevOps Life-Saver)

How to Convert Any CLI Output to JSON in 3 Lines of Code (DevOps Life-Saver)

If you've ever piped curl, grep, or awk into a Frankenstein pipeline just to parse CLI output, this is for you.

I spent 6 months debugging automation scripts that broke every time a CLI tool updated its output format. Then I found jc — a Python CLI tool with 8,500+ GitHub stars that converts almost ANY command-line output to JSON.

The Problem

Standard CLI tools output human-readable text. Great for humans. Terrible for automation.

Example: ifconfig output looks like this:

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.100  netmask 255.255.255.0  broadcast 192.168.1.255
Enter fullscreen mode Exit fullscreen mode

Good luck parsing that reliably across different systems.

The Solution: jc

Install:

pip install jc
Enter fullscreen mode Exit fullscreen mode

Use:

ifconfig | jc --ifconfig
Enter fullscreen mode Exit fullscreen mode

Output:

[
  {
    "name": "eth0",
    "flags": 4163,
    "state": ["UP", "BROADCAST", "RUNNING", "MULTICAST"],
    "mtu": 1500,
    "ipv4_addr": "192.168.1.100",
    "ipv4_mask": "255.255.255.0",
    "ipv4_bcast": "192.168.1.255"
  }
]
Enter fullscreen mode Exit fullscreen mode

Now you can pipe that into jq, store it in a database, or pass it to your monitoring stack.

What jc Supports

Over 80 CLI tools:

  • ps, top, netstat, lsof
  • dig, nslookup, ping
  • df, mount, lsblk
  • iptables, route, arp
  • git log, git diff
  • Even custom log files

Full list: https://github.com/kellyjonbrazil/jc#parsers

Real-World Use Case

I use this to monitor disk usage across 50+ servers:

ssh user@server "df -h | jc --df" | jq '.[] | select(.use_percent > 80)'
Enter fullscreen mode Exit fullscreen mode

Alerts me when any disk hits 80% capacity.

Before jc: 40 lines of regex hell.

After jc: 1 line.

Why This Matters for DevOps

  • Automation scripts don't break when CLI output format changes
  • Monitoring dashboards can ingest structured data
  • No more brittle regex parsing
  • Works across Linux, macOS, and Windows

Advanced: Parsing Custom Output

If jc doesn't support your tool, you can write custom parsers. The repo has great docs:

https://github.com/kellyjonbrazil/jc


Bonus: Prompt Pack for CLI Automation

I compiled 50 AI prompts specifically for DevOps engineers who automate CLI workflows. Covers:

  • Writing error-resistant parsing scripts
  • Handling edge cases in CLI output
  • Debugging JSON conversion issues
  • Optimizing performance for large outputs

Available here: https://payhip.com/vitalflowstore

(Use the prompts with ChatGPT, Claude, or Gemini — saves ~10 hours/week)


Quick Start

# Install
pip install jc

# Try it
ps aux | jc --ps

# Combine with jq
ps aux | jc --ps | jq '.[] | select(.user == "root")'
Enter fullscreen mode Exit fullscreen mode

Found this useful? Star the repo: https://github.com/kellyjonbrazil/jc

Questions? Drop them below.


Resources

If you found this useful, I put together CLI Data Parsing Prompt Pack — a complete resource that goes much deeper on everything covered here. It's a one-time download that pays for itself the first time you use it.

Check it out: https://payhip.com/vitalflowstore


What's your biggest challenge with this topic? Drop it in the comments.

Top comments (0)