Format JSON from your clipboard with jq
Using jq to format JSON on the clipboard
A practical tip for developers: use jq to pretty-print JSON copied to the clipboard. The article shows a macOS alias using pbcopy/pbpaste, then adapts it for Linux with xclip, adding error handling to avoid overwriting the clipboard if the content isn't valid JSON.
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.
- 1-more
I do a lot of this and `pbpaste | sed s/^ // | pbcopy` and things like that. It just fits my brain better. What's nice about raycast as a macOS pasteboard manager is that whatever you last selected via Cmd-Shift-V is the output of `pbpaste`. I'm not 100% sure how that is affected by copying images though.
- bilkow
Loved it! I made this version for `fish` and `wl-clipboard`:
function clipboard-format-json
# Inspired by https://chris48s.github.io/blogmarks/posts/2021/jsontidy/
if set -l formatted "$(wl-paste | jq '.')"
wl-copy $formatted
end
end
By saving it to .config/fish/functions/clipboard-format-json.fish, it should immediately work
- _flux
I use similar approach for fixing up clipboard contents with
tr '\n' ' ' | sed 's/\t/ /g; s/ */ /g'
But also I switched to xsel at one point, as xclip doesn't seem to deal as well with unicode. It could be xclip has been fixed since then, though.