LinuxCommandLibrary

gron

Transform JSON into discrete assignments

TLDR

Process JSON file into individual assignments

$ gron [path/to/file|url]
copy

Don't sort output data
$ gron --no-sort [path/to/file|url]
copy

Disable certificate validation
$ gron [[-k|--insecure]] [url]
copy

Display values of gron assignments
$ gron [[-v|--values]] [path/to/file|url]
copy

Turn assignments converted with gron back into JSON
$ gron [[-u|--ungron]] [path/to/file|url]
copy

Process individual lines of input as separate JSON objects
$ gron [[-s|--stream]] [path/to/file|url]
copy

Represent processed data as a JSON stream
$ gron [[-j|--json]] [path/to/file|url]
copy

SYNOPSIS

gron [OPTIONS] [FILE]
gron -u [OPTIONS] [FILE]

PARAMETERS

-u, --ungron
    Reconstructs JSON from the gron format. This effectively reverses the gron transformation, converting assignments back into a valid JSON document.

-a, --all-values
    Includes all values in the output, even if they are null or empty arrays/objects. By default, gron omits such values.

-s, --stream
    Reads the input as a continuous stream of JSON objects, processing each one individually. This is useful for handling large log files or continuous data streams.

-j, --json
    When used with -u (ungron), outputs the reconstructed JSON in a compact, single-line format.

-y, --yaml
    When used with -u (ungron), outputs the reconstructed data in YAML format instead of JSON.

-c, --color
    Enables colorized output for better readability of the gron assignments in a terminal.

-m, --monochrome
    Disables colorized output, which can be useful when piping gron output to other commands or in terminals that do not support colors.

-p, --obj-prefix <prefix>
    Adds a custom string prefix to all variable names in the gron output. For example, --obj-prefix 'mydata' would result in assignments like mydata.json.key = ....

--no-sort
    Disables sorting of object keys in the output. This can lead to more predictable output order, especially useful for diffing or consistent processing.

--indent <spaces>
    Specifies the number of spaces to use for indentation when outputting formatted JSON or YAML (typically with -u).

-h, --help
    Displays a help message with available options and exits.

-v, --version
    Prints the gron version information and exits.

DESCRIPTION

gron is a powerful command-line utility that transforms JSON data into a series of discrete, newline-separated assignments. This unique transformation flattens complex, nested JSON structures, making them exceptionally amenable to standard Unix text processing tools such as grep, awk, and sed.

Instead of navigating intricate JSON paths, gron represents each value with a unique, dot-separated path, like json.data[0].id = 123;. This flat, assignment-based format allows users to easily search for specific values, patterns, or modify data within JSON documents using familiar text commands, eliminating the need for specialized JSON parsers for many tasks.

A key feature of gron is its 'round-trip' capability. After processing or filtering the gron output with other tools, the modified data can be piped back into gron -u (or the alias ungron) to reconstruct the original or modified JSON structure. This makes gron an incredibly versatile tool for debugging, inspecting API responses, automating data transformations, and generally leveraging the Unix philosophy on structured data.

CAVEATS

While powerful, gron can produce very large output files for extensive JSON documents, potentially impacting performance or memory usage. Modifying the gron output manually requires careful adherence to its specific assignment syntax; any errors can lead to parsing failures when attempting to ungron back into JSON. gron expects valid JSON input and may not handle malformed data gracefully.

ROUND-TRIP TRANSFORMATION

gron's fundamental strength lies in its ability to perform a 'round-trip' transformation. You can convert JSON to the gron format, process or filter it using standard Unix tools (like grep, sed, awk), and then pipe the modified gron output back into gron -u (or ungron) to reconstruct a valid, modified JSON document. This makes it incredibly flexible for data manipulation workflows that benefit from the power of line-oriented text processing.

INTEGRATION WITH UNIX TOOLS

gron is explicitly designed for seamless interoperability with traditional Unix command-line tools. By flattening structured JSON into simple, line-oriented text assignments, it allows users to apply a vast ecosystem of text processing commands that were not originally designed for hierarchical data. This integration makes otherwise complex JSON manipulation tasks straightforward and highly scriptable, fitting perfectly within the Unix philosophy of small, specialized tools working together.

HISTORY

gron was created by Tomas Lindberg and first released around 2016. Its development was inspired by the need for a simple, Unix-philosophy-aligned tool to make JSON data easily searchable and manipulable with existing text-processing utilities. Before gron, users often had to rely on more complex tools like jq for even basic searches, or write custom scripts. gron filled a niche by allowing users to directly leverage their existing grep/sed/awk knowledge on JSON structures, providing a simpler and highly effective alternative for common use cases. Its adoption grew rapidly within the DevOps, system administration, and data analysis communities for its utility in handling JSON-formatted log files and API responses.

SEE ALSO

jq(1), grep(1), sed(1), awk(1), curl(1)

Copied to clipboard