brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: python3 -m json.tool makes ugly JSON readable in one pipe

Python
~/tips/tip-python-json-tool

curl -s https://example.com/api | python3 -m json.tool

Takes messy one-line JSON and prints it indented, so you can read it and spot typos.

Python is on almost every machine I touch, and it comes with a JSON pretty-printer. No jq to install first.

Pipe any JSON into it:

curl -s https://example.com/api | python3 -m json.tool
python3 -m json.tool config.json

You get indented, readable output. If the JSON is broken, it stops with the line and column of the error, which makes it a quick validator as well.

The caveat is that it is only a formatter. For picking out one field, filtering or editing, jq is the better tool. Also note that it keeps the key order but can write non-ASCII characters as escapes; add --no-ensure-ascii (Python 3.9 and newer) if you want to see them as they are.

Further reading: json.tool (Python documentation), On this site: python3 -m http.server shares a folder over HTTP in one line, On this site: –json-schema makes claude -p return JSON you can trust

More Python tips import asyncio from enum import StrEnum, auto except* ValueError as eg: import pytest pytest --maxfail=2 All Python tips (107) All topics

Link