Skip to content

API

All imports here are exported as public interface.

Gron's command line interface (CLI).

Attributes:

Name Type Description
parser ArgumentParser

main()

Gron's CLI.

This method reads the arguments for the command line interface and runs gron.

Source code in gron/__main__.py
30
31
32
33
34
35
36
37
38
39
def main() -> None:
    """Gron's CLI.

    This method reads the arguments for the command line interface and
    runs `gron`.

    """
    args = parser.parse_args()
    data = args.file.read()
    print(gron(data))

Gron's core functions.

convert(name)

Convert path name into valid JSON.

Parameters:

Name Type Description Default
name str

a path name

required

Returns:

Type Description
str

valid JSON path

Source code in gron/gron.py
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
def convert(name: str) -> str:
    """Convert path name into valid JSON.

    Parameters
    ----------
    name
        a path name

    Returns
    -------
    str
        valid JSON path

    """
    if '-' in name or ' ' in name:
        return f'["{name[1:]}"]'
    return name

gron(input_)

Transform JSON into parseable str.

This method takes a JSON string and transforms it into a grepable equivalent form.

Parameters:

Name Type Description Default
input_ str

JSON

required

Returns:

Type Description
str

Transformed output

Source code in gron/gron.py
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
def gron(input_: str) -> str:
    """Transform JSON into parseable str.

    This method takes a JSON string and transforms it into a grepable
    equivalent form.

    Parameters
    ----------
    input_
        JSON

    Returns
    -------
    str
        Transformed output

    """
    python = json.loads(input_)
    output = walk(python, 'json')
    return output

walk(node, name)

Translate Python element to JSON.

This method recursively visits each element of a Python object and returns the JSON representation.

Parameters:

Name Type Description Default
node Any

A python object (e.g. dict, list, int, etc)

required
name str

The name (i.e. path) of the parent element

required

Returns:

Type Description
str

Transformed JSON for this element

Source code in gron/gron.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def walk(node: Any, name: str) -> str:
    """Translate Python element to JSON.

    This method recursively visits each element of a Python object and
    returns the JSON representation.

    Parameters
    ----------
    node
        A python object (e.g. dict, list, int, etc)
    name
        The name (i.e. path) of the parent element

    Returns
    -------
    str
        Transformed JSON for this element

    """
    if node is None:
        return f"{name} = null;"
    elif isinstance(node, bool):
        return f"{name} = {str(node).lower()};"
    elif isinstance(node, str):
        return f'{name} = "{node}";'
    elif isinstance(node, bytes):
        return f'{name} = "{node!r}";'
    elif isinstance(node, dict):
        res = []
        res.append(f"{name} = {{}};")
        for k, v in sorted(node.items()):
            res.append(walk(v, name + convert('.' + k)))
        return '\n'.join(sorted(res))
    elif isinstance(node, list | tuple):
        res = []
        res.append(f"{name} = [];")
        for i, e in enumerate(node):
            res.append(walk(e, name + convert(str([i]))))
        return '\n'.join(res)

    else:
        return f"{name} = {node!r};"

Module providing gron's version.