LinuxCommandLibrary

json_pp

Pretty-print JSON data

SYNOPSIS

json_pp [-v|-V] [--pretty-print | --no-pretty-print] [--indent | --no-indent] [--canonical] [--sort-keys] [--bare-value] [file...]

PARAMETERS

--pretty-print
    Outputs JSON in a human-readable, indented format. This is typically the default behavior of the command.

--no-pretty-print
    Outputs JSON in a compact, unindented format without extra whitespace, effectively disabling the default pretty-printing.

--indent
    Enables indentation of the output. This option is usually active by default when pretty-printing is enabled.

--no-indent
    Disables indentation, leading to output on a single line where possible. If --pretty-print is also active, it will still add newlines but no spaces.

--canonical
    Outputs JSON in a canonical form: with all keys sorted, no whitespace, and numerically stable floating-point numbers. This option implicitly enables --no-pretty-print and --no-indent.

--sort-keys
    Sorts all hash (object) keys alphabetically in the output. This is beneficial for ensuring consistent ordering, useful for diffing or caching JSON data.

--bare-value | --allow_nonref
    Allows the top-level JSON entity to be a scalar (e.g., a number, string, boolean, or null) instead of strictly requiring it to be an object or an array.

-v | -V
    Prints the version information of json_pp and then exits.

DESCRIPTION

json_pp is a command-line utility designed to format JSON (JavaScript Object Notation) data, making it significantly more readable for humans. It processes raw JSON input, typically sourced from standard input or a specified file, and outputs a nicely indented and structured version to standard output.

This tool proves particularly useful for debugging API responses, inspecting configuration files, or simply organizing large JSON payloads into a more digestible format. It provides various options to control the output format, including indentation levels, alphabetical sorting of keys, and outputting in a canonical (minimal whitespace) form. json_pp is distributed as part of the JSON::PP Perl module, ensuring its wide availability on systems with a Perl installation.

CAVEATS

Perl Dependency: json_pp is a Perl script and thus requires a Perl interpreter along with the JSON::PP module to be installed on the system.

Error Handling: If the input JSON is malformed or invalid, json_pp will typically output an error message to standard error and exit. The exact error message format may vary.

Performance with Large Files: For extremely large JSON files, its performance might be somewhat slower compared to similar tools written in compiled languages, though it generally remains efficient enough for most common use cases.

Default Behavior Variation: The precise default pretty-printing behavior, such as the specific indentation level used, can sometimes vary slightly between different versions of the JSON::PP module.

USAGE EXAMPLES

Here are some common ways to use json_pp:

Pretty-print from standard input:
echo '{"name":"Alice","age":30}' | json_pp

Pretty-print a file to another file:
json_pp mydata.json > formatted_data.json

Pretty-print an API response with sorted keys:
curl https://api.example.com/data | json_pp --sort-keys

Convert to canonical form:
cat ugly.json | json_pp --canonical

HISTORY

json_pp originated as a command-line utility bundled with the JSON::PP Perl module. JSON::PP (Pure Perl JSON) was developed to provide JSON serialization and deserialization capabilities implemented entirely in Perl, without reliance on external C libraries, thus ensuring high portability. As JSON became a ubiquitous data interchange format in web development and system administration, tools like json_pp became indispensable for developers and system administrators working with JSON data on the command line. Its ongoing development is closely tied to the evolution and maintenance of the JSON::PP module itself, managed within the Perl community on CPAN (Comprehensive Perl Archive Network). It serves as a pure-Perl alternative to the JSON::XS module and its accompanying json_xs utility, which provides similar functionality but leverages C bindings for potentially faster performance.

SEE ALSO

jq(1), python -m json.tool, json_xs(1), cat(1)

Copied to clipboard