Skip to content

Color in json.tool CLI #131952

Closed
Closed

Description

@raztd

Feature or enhancement

Proposal:

apologies if this is not the right place to ask for enhancement requests. since python3.13 added support for colorization in repl, i was wondering if some basic colorization could be added for the python's cli json tool? jq[0] does this, the keys are bolded and have a different color from the values.

if this gets implemented i think a lot of people will be happy to just use python and not have to install yet another library to fulfill their needs :-)

[0] https://static1.howtogeekimages.com/wordpress/wp-content/uploads/2020/01/5-7.png

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Linked PRs

Activity

  1. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Mar 31, 2025
  2. ZeroIntensity commented on Mar 31, 2025

    @ZeroIntensity
    Member
  3. sobolevn commented on Apr 1, 2025

    @sobolevn
    Member

    I think that adding colors by default can possibly break piping on some shells / systems. But, I am not 100% sure.

    Like get_json | python -m json.tool | expects_json_text

  4. hugovk commented on Apr 1, 2025

    @hugovk
    Member

    By default, the _colorize module does check if we're in a tty before enabling colour:

    cpython/Lib/_colorize.py

    Lines 109 to 112 in 3b3720f

    try:
    return os.isatty(file.fileno())
    except io.UnsupportedOperation:
    return hasattr(file, "isatty") and file.isatty()

    Compare the output of python3.14 -m calendar and python3.14 -m calendar | cat

  5. ZeroIntensity commented on Apr 1, 2025

    @ZeroIntensity
    Member

    Compare the output of python3.14 -m calendar

    Speaking of which, is that extra highlighted space before the 1 supposed to be there?

    Image

    The assymetry looks a little funky to me.

  6. Eclips4 commented on Apr 1, 2025

    @Eclips4
    Member

    Speaking of which, is that extra highlighted space before the 1 supposed to be there?

    Image

    The assymetry looks a little funky to me.

    I guess so. At least so-called cal does the same:

    Image
  7. srinivasreddy commented on Apr 2, 2025

    @srinivasreddy
    Contributor

    asymmetry happens on MacOS too.

    Image
  8. hugovk commented on Apr 2, 2025

    @hugovk
    Member

    The highlight includes the unprinted 0 prefix.

  9. nineteendo commented on Apr 2, 2025

    @nineteendo
    Contributor

    Like this?

    Image

  10. raztd commented on Apr 6, 2025

    @raztd
    Author

    @nineteendo thank you for taking the time to look into it. couple of notes: 1) unlike jq and python's json tool, i see your solution is not recursive (objects and arrays inside a json object are not nicely displayed, but everything is in one line) 2) i'm not a fan of that shade of bright blue

  11. nineteendo commented on Apr 6, 2025

    @nineteendo
    Contributor

    your solution is not recursive

    That's because -L1 sets --maximum-indent-level to 1.

    i'm not a fan of that shade of bright blue

    I'm using the same colours as jq, this is just how Powershell renders them.

  12. changed the title [-]colorization in cli json tool[/-] [+]Color in json.tool CLI[/+] on Apr 6, 2025
  13. added a commit that references this issue on Apr 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions