How To Copy Json Straight To Clipboard From The Terminal

Published

December 31, 2022

what i learned

Piping output to pbcopy to copy and paste output from the terminal

Here’s how I can grab the last 10 elements of a JSON array and copy them to my clipboard.

jq '.[-10:]' mydata.json | pbcopy

how i learned

I’ve been working with my spotify streaming history data and it’s a lot of nested data so I’ve been using jq a lot.

I’m working on restructuring a complicated nested JSON so I went to <jqplay.org> and I needed some sample data. I remember reading a tweet or maybe a tip on a Medium article about piping to pbcopy so I figured I’d try it and document it.

The final code looked more like

jq '.[-5:]' interim/streaming_history.json | pbcopy

reference