How to colorize json on terminal?

syntax-highlight
json
Tags: #<Tag:0x00007f1149212f30> #<Tag:0x00007f1149212c60>

#1

Hi!

I’m writing an elixir script that output JSON files on STDOUT, I’d like to offer syntax colorization, like the one of python project HTTPie which use Pygments library.

Do you know if there’s an elixir or erlang equivalent? If not, have you advices to built it?

Thanks.


#2

Just pipe it through jq. this is a wheel that does not need reinventing.


#3

I didn’t know jq, thank you it will be useful on a day2day basis. My output won’t be 100% JSON, so piping to jq is not an option. Since it’s pure C, I’ll check if I’m able to create elixir bindings for jq…

Thanks you, @bbense

Waiting for checking that point, if someone else have alternative to jq… :wink:


#4

If it’s not JSON, don’t ask for JSON.

If you have something to colourize, perhaps makeup can help you.

You’ll need to write a parser for your format though, as well as a formatter for the terminal, but at least the latter can be put under open source or even get integrated into makeup, as colouring into a terminal is something I love pygmeents for.


#5

There is IO.ANSI in the stdlib. The tricky part about colorization is knowing when not to do it. If you are writing a unix CLI tool, colorizing the output always makes it difficult to pipe the output of that tool into another tool. IO.ANSI mostly gets this correct