Python Gron
`gron` is a Python library and command-line tool that transforms JSON into a more "greppable" format, where each field is represented as a distinct, path-based line, making it easier to search and manipulate. It also provides functionality to convert the "gron" format back to JSON. The current version is 1.4.0, and it sees active development with minor feature releases.
Common errors
-
AttributeError: module 'gron' has no attribute 'json_to_gron'
cause The `gron` library was installed but the functions are not being accessed correctly, or an older version without these functions might be installed, or another module is shadowing the `gron` name.fixEnsure `pip install gron` is successful and you are importing the `gron` module, then accessing its functions via `gron.json_to_gron()` and `gron.gron_to_json()`. Verify the installed version with `pip show gron`. -
gron.exceptions.GronParsingError: Unable to parse gron input at line X (e.g., at 'unexpected token Y')
cause The input string provided to `gron.gron_to_json()` does not conform to the expected `gron` syntax.fixReview the `gron` input string for syntax errors, such as missing semicolons, incorrect value assignments, unquoted strings that should be quoted, or invalid path definitions. The `gron` format is strict.
Warnings
- gotcha The `gron_to_json` function expects input strictly adhering to the `gron` format (e.g., `json.key = "value";`). Malformed gron strings or non-gron input will result in parsing errors.
- gotcha The `gron` format flattens nested JSON objects and arrays into individual, path-based lines. Users expecting a re-structured but still hierarchical JSON output from `json_to_gron` will find the output is primarily for line-by-line processing.
Install
-
pip install gron
Imports
- gron
import gron
- json_to_gron
from gron import json_to_gron
- gron_to_json
from gron import gron_to_json
Quickstart
import gron
import json
data = {'name': 'Alice', 'age': 30, 'address': {'street': '123 Main St', 'city': 'Anytown'}}
json_string = json.dumps(data, indent=2)
# Convert JSON to gron format
gron_output = gron.json_to_gron(json_string)
print("--- Gron Output ---")
print(gron_output)
# Example gron string (usually from json_to_gron output)
gron_lines = """
json = {};
json.name = "Alice";
json.age = 30;
json.address = {};
json.address.street = "123 Main St";
json.address.city = "Anytown";
"""
# Convert gron format back to JSON
json_back = gron.gron_to_json(gron_lines)
print("\n--- JSON Back ---")
print(json.dumps(json_back, indent=2))