formatting JSON

1,672 views
Skip to first unread message

Aaron Miller

unread,
Nov 12, 2015, 3:04:25 PM11/12/15
to iterm2-discuss
is iterm2 able to 'prettify' JSON before it displays it?

Rick Hornsby

unread,
Nov 12, 2015, 3:24:51 PM11/12/15
to iterm2-...@googlegroups.com

> On Nov 12, 2015, at 11:37, Aaron Miller <0101...@gmail.com> wrote:
>
> is iterm2 able to 'prettify' JSON before it displays it?

I don't think iTerm knows what the content-type is before it displays it. iTerm would rely on the escape codes and other methods of the underlying application (bash, vim, etc) to determine how to display something.

You might check out underscore-cli

https://github.com/ddopson/underscore-cli

I thought it was available from brew, but I'm not seeing it there.

You can take whatever your JSON is and pipe it unix-style to underscore-cli to get pretty JSON. There are also one-liners for Ruby, Perl, Python etc that will take JSON on stdin and prettyprint it for you. Ruby example:

ruby -rjson -e 'puts JSON.pretty_generate(JSON.parse(STDIN.read))'

That is:

$ echo '{ "array": [1, 2, 3, { "sample": "hash"} ], "foo": "bar" }' | ruby -rjson -e 'puts JSON.pretty_generate(JSON.parse(STDIN.read))'
{
"array": [
1,
2,
3,
{
"sample": "hash"
}
],
"foo": "bar"
}

George Nachman

unread,
Nov 12, 2015, 3:30:52 PM11/12/15
to iterm2-...@googlegroups.com
Detecting JSON on the fly would be expensive and error-prone, but this is an idea that crosses my mind from time to time. I just haven't figured out a sane way to do it yet.

}

--
You received this message because you are subscribed to the Google Groups "iterm2-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to iterm2-discus...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Petite Abeille

unread,
Nov 12, 2015, 5:30:13 PM11/12/15
to iterm2-...@googlegroups.com

> On Nov 12, 2015, at 6:37 PM, Aaron Miller <0101...@gmail.com> wrote:
>
> is iterm2 able to 'prettify' JSON before it displays it?

Give jq a try:

https://stedolan.github.io/jq/

Rick Hornsby

unread,
Nov 12, 2015, 5:38:44 PM11/12/15
to iterm2-...@googlegroups.com
cool, haven't heard of this before. thanks for the recommendation!

Looks like jq is available via brew

aztec:~ rhornsby$ brew info jq
jq: stable 1.5 (bottled), HEAD
Lightweight and flexible command-line JSON processor
https://stedolan.github.io/jq/


Nick Richards

unread,
Oct 25, 2018, 4:20:58 PM10/25/18
to iterm2-discuss
How about a pop-up window which can both unescape (\"key\" : \"value\" => "key" : "value") and format any block of text alt-clicked upon? 

iTerm2 currently can recognize links you alt-click on. How about it tries to find the biggest enclosed JSON or escaped-JSON, spanning one or multiple lines, and pop-up a little helper scrollable window?

I'd REALLY like that.

George Nachman

unread,
Oct 26, 2018, 3:23:31 PM10/26/18
to iterm2-...@googlegroups.com
I'm glad this thread came up again today as this capability will exist in iTerm2 version 3.3 as a status bar extension. Here's a script that implements it:


This stuff is still in development and will probably break from time to time, but I'd love to get feedback on it.

Nick Richards

unread,
Oct 26, 2018, 6:08:02 PM10/26/18
to iterm2-...@googlegroups.com
A happy coincidence. Thanks for the quick response to my inquiry.

Fascinating. Just watched the MOV on Patreon by you George (link https://www.patreon.com/posts/custom-status-21946224). This should do the trick. 

Seems to me that my use-case could be implemented by me with v3.3: "Unescape and Re-Pretty until satisfied." I'm often dealing with encoded JSON(JSON(JSON))) and the bracket range-selection (Option+Cmd+DoubleClick on the bracket) + browser pop-up for collecting / rendering web content + scripting features would facilitate that usage nicely.

Possibly I'd customize the script and add controls (similar to the iTerm2v3.3 status bar?) for the web view itself to be able to "re-pretty and unescape" the body, "going deeper" so to speak. Enabling how one may recursively Go To Definition on an unknown symbol, then when satisfied one may unwind their stack back to whatever code they were developing.

OT: White on black works fine for a new webview. Might be nice in the future if the webview could adopt some of the color scheme as an injectable prebuilt css derived from the current Terminal Profile.

https://iterm2.com/python-api/statusbar.html#iterm2.CheckboxKnob is just a button? or is a thing that is associated with data too?

I guess defocusing, or ESC withdraws the webview?

Cheers,
Nick


You received this message because you are subscribed to a topic in the Google Groups "iterm2-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/iterm2-discuss/FSUYi9uVfJY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to iterm2-discus...@googlegroups.com.

George Nachman

unread,
Oct 26, 2018, 6:40:39 PM10/26/18
to iterm2-...@googlegroups.com
On Fri, Oct 26, 2018 at 3:08 PM Nick Richards <nric...@gmail.com> wrote:

Possibly I'd customize the script and add controls (similar to the iTerm2v3.3 status bar?) for the web view itself to be able to "re-pretty and unescape" the body, "going deeper" so to speak. Enabling how one may recursively Go To Definition on an unknown symbol, then when satisfied one may unwind their stack back to whatever code they were developing.

The only unsolved problem is communicating from the web view back to your script. My best idea is to use JavaScript websockets to run an api client in the web view, and have a way for two api clients to pass each other messages. Kinda complicated though.


OT: White on black works fine for a new webview. Might be nice in the future if the webview could adopt some of the color scheme as an injectable prebuilt css derived from the current Terminal Profile.

Yeah, looks jarring in dark mode. I’ll look into it.

https://iterm2.com/python-api/statusbar.html#iterm2.CheckboxKnob is just a button? or is a thing that is associated with data too?

Knobs are for configuring status bar components through preferences. 


I guess defocusing, or ESC withdraws the webview?

Yep

Nick Richards

unread,
Oct 26, 2018, 9:33:13 PM10/26/18
to iterm2-...@googlegroups.com
Groovy.

If you do need two way communication Web sockets does sound to me like a good idea. Using other browser-technologies like POST'ing to special URLs means dealing with caching and security (XSS / CORS ?). I've had to use that technique for an Objective-C mobile app which hosted a JS webview, and it was painful + slow due to character conversion / JavaScript runtime overhead.

Perhaps it would be fun to create a JS WebSockets client & server, get that working, then a Python WebSockets client & server, get that working, then connect the ports together to get JS & Python talking. Not that I have any experience with WebSockets libraries.

Looking forward to learning more about what you discover and decide upon.

And looking forward to Knobs :) Thanks for the consideration around the CSS, is really just a thought.

Cheers,
Nick

George Nachman

unread,
Oct 29, 2018, 1:57:36 PM10/29/18
to iterm2-...@googlegroups.com
iTerm2 runs a websocket server on port 1912, which is how scripts communicate with it. Your javascript could act like a script and set a user-defined variable, which are easy for scripts to listen for. You'll need javascript protobufs (fun!), as I have only implemented a friendly API for Python. The API is documented in the protobuf definition: https://github.com/gnachman/iTerm2/blob/master/proto/api.proto

There's an old and perhaps working node.js example here: https://github.com/gnachman/iTerm2/tree/master/tests/websocket

Reply all
Reply to author
Forward
0 new messages