◐ Shell
clean mode source ↗

gh-131952: Add color to the `json` CLI by tomasr8 · Pull Request #132126 · python/cpython

@tomasr8

@tomasr8 tomasr8 commented

Apr 5, 2025

edited by bedevere-app Bot

Loading

Example:

image

I'm open to changing the colors. For now it's green for strings, yellow for numbers and cyan for true/false/null.

brianschubert

StanFromIreland

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How much of a performance impact does this have?

picnixz

@picnixz

How much of a performance impact does this have?

It doesn't matter here. json.tool is more of a visualization tool. If users don't want to colorize the output they can also disable it using the NO_COLOR env. var IIRC. OTOH, we could add an explicit --pretty flag to enable colorization instead of making the default.

@tomasr8

OTOH, we could add an explicit --pretty flag to enable colorization instead of making the default.

I think we might not need that, can_colorize can already detect most situations where color is not desirable (e.g. not a TTY). You can also set NO_COLOR to turn off colors unconditionally.

@AA-Turner AA-Turner changed the title gh-131952: Add color to the json.tool CLI. gh-131952: Add color to the json.tool CLI

Apr 5, 2025

@picnixz

What about CLIs? they would be broken if data is piped?

@tomasr8

What about CLIs? they would be broken if data is piped?

AFAIU they wouldn't (and local testing confirms that), see this comment from Hugo: #131952 (comment)

nineteendo

danielhollas

nineteendo

picnixz

@raztd

@tomasr8

@tomasr8 thank you for taking the time to look into it. couple of notes regarding the keys 1) could you make them a different color from the string values? 2) could you make them bold?

this is what jq does and i think it provides a good ux https://static1.howtogeekimages.com/wordpress/wp-content/uploads/2020/01/5-7.png

That should be in theory possible, we can distinguish keys from string literals since keys are always followed by :. I will make a couple different color versions and then we can decide which one looks the best.

hugovk

Comment on lines +53 to +54

with_colors = can_colorize()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable isn't doing much, shall we just call the function directly?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may have been too cautious here. Without the variable, the function would be called in a loop and I was worried the result could change in between invocations, though that is really unlikely. If you prefer I can remove it?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I applied your other suggestion which already removed it so never mind 😄

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, that's a good point: there could be a lot of objs as JSONL files can be quite long. Let's add with_colors back, but just outside the loop.

@python-cla-bot

All commit authors signed the Contributor License Agreement.

CLA signed

@tomasr8

Here's a version with keys in a different color and a bit better color contrast:

@AA-Turner

The green and cyan colours here look bad on the white background, which is the default for Windows Terminal, PyCharm's in-built terminal, etc. Could you darken them?

@tomasr8

The previous green and cyan were the 'intense' variants. Here's the normal one. Below is also a version where the colors are bold which I think looks even better.

With bold colors:

@AA-Turner

I also prefer the bold version, thanks.

A

@tomasr8

Thanks for your feedback! I think it looks much better now :) I added the bold colors and updated tests. Would you mind taking another look?

@raztd

@tomasr8

could you bold the keys and leave the values regular?

I think I prefer the bold colors. In my opinion, they are more legible when bold, especially with the light theme.

@raztd

could you bold the keys and leave the values regular?

I think I prefer the bold colors. In my opinion, they are more legible when bold, especially with the light theme.

in the last pair of screenshots, i see only the values as bold. the keys look regular. my proposal was to invert those. or do you want to make the keys bold, too? i think that could work

AA-Turner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hugovk

Looking good. One suggestion, jq uses default colour/uncoloured for numbers:

image

Which is more readable with light background than yellow?

image

@hugovk

Please could you also add this to What's New, and mention the colour env vars, like the other similar entries?

@tomasr8

in the last pair of screenshots, i see only the values as bold. the keys look regular. my proposal was to invert those. or do you want to make the keys bold, too? i think that could work

I find the color contrast for the intense blue variant better compared to just bold blue:

One suggestion, jq uses default colour/uncoloured for numbers:
Which is more readable with light background than yellow?

Looks good:

Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>

@tomasr8

Please could you also add this to What's New, and mention the colour env vars, like the other similar entries?

Added - I reused your entry from the calendar module ;)

hugovk

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! 🎨

StanFromIreland

Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>

StanFromIreland