The article details a method for formatting JSON data stored in the system clipboard using the command-line tool `jq`. This utility is commonly used for processing JSON data, and this specific application focuses on its pretty-printing capabilities.
For macOS users, the solution utilizes `pbpaste` to retrieve clipboard content and `pbcopy` to place the formatted output back into the clipboard. The initial alias provided is `jsontidy="pbpaste | jq '.' | pbcopy"`.
Linux users can adapt this functionality by substituting `xclip` for the macOS-specific commands. The equivalent command for Linux is `xclip -selection clipboard -o | jq '.' | xclip -selection clipboard`.
A key improvement introduced is error handling. Without it, if the clipboard content is not valid JSON, `jq` would exit with an error, and the clipboard would be overwritten with an empty string. The enhanced alias addresses this by ensuring the original clipboard content is retained if `jq` fails to parse the input.
The refined alias for Linux, incorporating error handling, is `alias jsontidy="xclip -selection clipboard -o | (jq '.' || xclip -selection clipboard -o) | xclip -selection clipboard"`. This command attempts to format the JSON; if it fails, it passes the original clipboard content through, preventing data loss.
This technique provides developers with a convenient and efficient way to quickly format JSON data they encounter, such as API responses or configuration files, directly from their clipboard without needing to save it to a file or use a separate application. The error handling ensures that the process is non-destructive even when dealing with non-JSON text.
✨ This summary was generated by AI from the outlets' reporting listed below. It is not independently verified and may contain errors — check the original sources. How BrevFeed works →
One email each morning: the day's tech stories, clustered across outlets and summarized. No account needed.
One email a day. Unsubscribe in one click, any time.
Spend a few minutes, get the whole day. Every topic's top stories in one hands-free rundown — listen, watch, or read the transcript.
▶ Play today's briefNew every morning, and the back catalogue is archived by date.
This article provides a command-line alias to format JSON content from the clipboard using `jq` on macOS and Linux, including error handling for non-JSON input. This offers a quick way for developers to pretty-print JSON data directly from their clipboard.