← All stories
● Covered by 1 source · 1 reportLow impact1 neutral

Using jq with clipboard for JSON formatting on macOS and Linux

🔄 Updated 2h ago
New to BrevFeed? We gather this story from every outlet covering it into one summary — ranked by real-world impact, not just the latest headline — so you never miss what matters. What is BrevFeed? →

Key points

  • Alias uses `pbpaste` and `pbcopy` on macOS for clipboard interaction.
  • Alias uses `xclip` on Linux for clipboard interaction.
  • Includes error handling to prevent overwriting clipboard with empty string.
  • Formats JSON on clipboard or leaves it unchanged if not valid JSON.

Clipboard JSON Formatting with jq

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.

Platform-Specific Implementations

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`.

Robust Error Handling

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.

Impact for Developers

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 →

The daily brief

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.

Today's brief

Spend a few minutes, get the whole day. Every topic's top stories in one hands-free rundown — listen, watch, or read the transcript.

~30 min · 24 stories · Sep 02

▶ Play today's brief Listen on Spotify

New every morning, and the back catalogue is archived by date.

Reporting from

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.