Skip to content
HN On Hacker News ↗

Using jq to format JSON on the clipboard

▲ 61 points 20 comments by stefanvdw1 3d ago HN discussion ↗

Pangram verdict · v3.3

We believe that this entire text is human-written.

0 %

AI likelihood · overall

Human
100% human-written 0% AI-generated
SEGMENTS · HUMAN 1 of 1
SEGMENTS · AI 0 of 1
WORD COUNT 224
PEAK AI % 0% · §1
Analyzed
Sep 2
backend: pangram/v3.3
Segments scanned
1 windows
avg 224 words each
Distribution
100 / 0%
human / AI fraction
Verdict
Human
Pangram v3.3

Article text · 224 words · 1 segments analyzed

Human AI-generated
§1 Human · 0%

Jq is a great tool. I use it frequently, but this recent article from Sequoia McDowell still has some great tips in it. Well worth a read. One thing that stood out to me from this post was the snippet: I like this pretty-printing/formatting capability so much, I have an alias that formats JSON I've copied (in my OS "clipboard") & puts it back in my clipboard: alias jsontidy="pbpaste | jq '.' | pbcopy" That's crazy-useful, but I need to tweak it a bit. Firstly, as noted, pbcopy and pbpaste are mac utils. No worries. We can substitute xclip on Linux: xclip -selection clipboard -o | jq '.' | xclip -selection clipboard I spotted a minor problem here though: If I run this and the text on my clipboard isn't JSON then jq will exit with a non-zero code, output something like parse error: Invalid numeric literal at line 1, column 6 to stderr and nothing to stdout. Pipe doesn't care about the non-zero exit code though so we overwrite whatever was on the clipboard with an empty string. So lets add a bit of error handing: alias jsontidy="xclip -selection clipboard -o | (jq '.' || xclip -selection clipboard -o) | xclip -selection clipboard" Now jsontidy will format whatever is on the clipboard if it can parse as JSON but leave it alone otherwise.