LinuxCommandLibrary

json_pp

Pretty-print JSON data

SYNOPSIS

json_pp [options] [file ...]

PARAMETERS

--json
    Treat input as JSON (default)

--yaml
    Treat input as YAML

--indent N, -i N
    Set indentation spaces per level (default: 3)

--sorted-keys, -s
    Sort object keys alphabetically

--space-before
    Add space before ':'

--space-after
    Add space after ':'

--canonical
    Output in canonical form (sorted keys, consistent escapes)

--ascii
    Escape all non-ASCII characters

--pretty
    Enable pretty-printing (default)

--no-pretty
    Compact output without indentation

--validate, -v
    Validate input only, no output

--version
    Print version information

--help
    Show usage help

DESCRIPTION

json_pp is a Perl-based command-line utility for parsing and pretty-printing JSON data. It reads JSON (or YAML with options) from standard input or specified files and outputs it in a human-readable, indented format. Ideal for debugging APIs, formatting logs, or validating JSON structures.

By default, it indents with 3 spaces, sorts keys alphabetically if specified, and adds spaces around colons. Users can customize indentation, enable canonical ordering, escape non-ASCII characters, or switch to compact output. It also supports validation mode to check JSON without printing.

Part of Perl's core JSON::PP module, json_pp is lightweight, requiring no external dependencies beyond Perl. It's particularly useful in scripts for processing JSON from curl, pipelines, or file manipulation. For example, pipe API responses directly: curl example.com/api | json_pp. Handles large inputs efficiently and supports multiple files.

While powerful for simple tasks, for complex queries or transformations, tools like jq may be preferable.

CAVEATS

Requires Perl with JSON::PP module (core since Perl 5.14). May be slow on very large files compared to jq. No built-in filtering or querying.

COMMON USAGE

cat data.json | json_pp | less
json_pp --sorted-keys --indent 2 api.json

INPUT HANDLING

Reads from stdin if no files given; processes multiple files sequentially.

HISTORY

Introduced in Perl 5.14 (2011) as a frontend to the pure-Perl JSON::PP module, providing a convenient CLI for JSON handling without C dependencies like JSON::XS.

SEE ALSO

jq(1), yq(1), perl(1)

Copied to clipboard