JSON::Color(3)
π JSON::Color(3) β the manual page, but make it a group chat π
okay bestie, so this is JSON::Color(3), a Perl module, and it's literally the cutest thing: it makes your JSON pretty and colorful. ANSI escape sequences, but make it fashion. π
β¨ VERSION β¨
This document describes version 0.134 of JSON::Color (from the Perl distribution JSON-Color), released on 2023-07-02. yes she's been around a minute, she's got history, she's iconic. π
πΈ SYNOPSIS πΈ
use JSON::Color qw(encode_json);
say encode_json([1, "two", {three => 4}]);
# specify some options
say encode_json([1, "two", {three => 4}],
{
color_theme=>"bright256", # will be searched under ColorTheme::JSON::Color:: or ColorTheme::
});
# requires ColorTheme::Lens::Lighten
say encode_json([1, "two", {three => 4}],
{
color_theme=>["Lens::Lighten" =>{theme=>"JSON::Color::bright256"}],
});
that's it. that's the whole vibe. three lines of setup and you're glowing. β¨
π¦ DESCRIPTION π¦
This module generates JSON, colorized with ANSI escape sequences. like, it takes your boring little data structure and gives it main character energy.
want to change the color? check out the %theme in the source code, bestie. and here's the fun part β in theory you can also modify it to colorize using HTML. not saying you should, but the option is there, and that's lowkey amazing. π
π FUNCTIONS π
π encode_json($data, \%opts) => STR
Encode to JSON. and ngl, she's dramatic β this will die on error (e.g. when encountering non-encodeable data like Regexp or a file handle). she does not do ambiguity, she does not do "maybe", she just dies. periodt. π
Known options:
color_theme => STRβ pick a color theme, which is aColorThemecolor theme module, found under these namespaces:ColorTheme::JSON::Color::,ColorTheme::. the default isdefault_ansi, which is the moduleColorTheme::JSON::Color::default_ansi. peep the module source code for an example of writing your own color theme module, bc honestly you could. you could. πpretty => BOOL(default:0) β pretty-print. yes. make it readable. treat yourself. πΈlinum => BOOL(default:0) β show line number. for when you need to point at a specific line and be like "this one. this is the one." πsort_by => CODEβ if specified, then sorting of hash keys will be done using this sort subroutine. this is similar to thesort_byoption in the JSON module. note that the code is executed in theJSON::Colornamespace, example:
# reverse sort
encode_json(..., {sort_by => sub { $JSON::Color::b cmp $JSON::Color::a }});
another example, using Sort::ByExample:
use Sort::ByExample cmp => {-as => 'by_eg', example => [qw/foo bar baz/]};
encode_json(..., {sort_by => sub { by_eg($JSON::Color::a, $JSON::Color::b) }});
yes you get $JSON::Color::a and $JSON::Color::b instead of the usual $a and $b. it's giving β¨custom namespaceβ¨. iykyk.
π«Ά FAQ π«Ά
π What about loading?
Use JSON. short answer, sweet answer, we move on. π
π How to handle non-encodeable data?
Use Data::Clean::JSON. it handles the mess so you don't have to. iconic behavior. β¨
π Why do numbers become strings?
example:
% perl -MJSON::Color=encode_json -E'say encode_json([1, "1"])'
["1","1"]
π yeah. both of them got stringified. to detect whether a scalar is a number (e.g. differentiate between "1" and 1), the XS module Scalar::Util::LooksLikeNumber is used. this is set as an optional prerequisite, so you'll need to install it separately. after the prerequisite is installed:
% perl -MJSON::Color=encode_json -E'say encode_json([1, "1"])'
[1,"1"]
look at that. 1 is a number. "1" is a string. the way that works now. obsessed. π₯
π ENVIRONMENT π
πΈ NO_COLOR
If defined, will set default color theme to ColorTheme::NoColor. see <https://no-color.org/> for more details. respectfully, choosing no color is a valid choice and we support you. π«Ά
πΈ JSON_COLOR_COLOR_THEME
Set default color theme. Has precedence over COLOR_THEME. she's the bossy older sister of the two. π
πΈ COLOR_THEME
Set default color theme. the chill one. π
π HOMEPAGE π
Please visit the project's homepage at <https://metacpan.org/release/JSON-Color>. go say hi, she loves visitors. πΈ
π SOURCE π
Source repository is at <https://github.com/perlancar/perl-JSON-Color>. it's giving open source. it's giving community. we love to see it. π¦
β SEE ALSO β
To colorize with HTML, you can try Syntax::Highlight::JSON. bestie that's literally the whole plot twist.
Syntax::SourceHighlight can also colorize JSON/JavaScript to HTML or ANSI escape. it requires the GNU Source-highlight library. yes, an extra dependency. worth it tho. β¨
π AUTHOR π
perlancar <perlancar@cpan.org>
π CONTRIBUTOR π
Steven Haryanto <stevenharyanto@gmail.com>
π CONTRIBUTING π
to contribute, you can send patches by email/via RT, or send pull requests on GitHub. pick your lane bestie, both are valid. π«Ά
Most of the time, you don't need to build the distribution yourself. You can simply modify the code, then test via:
% prove -l
If you want to build the distribution (e.g. to try to install it locally on your system), you can install Dist::Zilla, Dist::Zilla::PluginBundle::Author::PERLANCAR, Pod::Weaver::PluginBundle::Author::PERLANCAR, and sometimes one or two other Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond that are considered a bug and can be reported to me. so if you had to do extra steps, that's not a you problem, that's a bug. periodt. π
π COPYRIGHT AND LICENSE π
This software is copyright (c) 2023, 2021, 2016, 2015, 2014, 2012 by perlancar <perlancar@cpan.org>.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. free software, free as in freedom, free as in slay. π
π BUGS π
Please report any bugs or feature requests on the bugtracker website <https://rt.cpan.org/Public/Dist/Display.html?Name=JSON-Color>.
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature. help me help you, bestie. the more info you give, the faster it gets fixed. π«Ά
okay so that's the whole page!! this doc ships with perl v5.42.0 and the page itself is dated 2025-07-25, and it's JSON::Color(3) β go forth and make your JSON glow β¨π
π¦
π¦ where this page comes from
- fedora 43: perl-JSON-Color 0.134-7.fc43 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms