Skip to main content

A simple tool for formatting JSON object.

Project description

JSON Formator

Build Status PyPI Version Installs Code Grade Test Coverage

jsonfmt is a CLI tool for pretty printing or compressing JSON objects.

It has the following features:

  1. Print JSON with hightlight and pretty format from files or stdin.
  2. Minimize JSON to a single line.
  3. Pick out parts from a large JSON via jsonpath.
  4. Format JSON to TOML or YAML.
  5. Conversion between these three formats.
  6. Copy the result to clipboard.

Install

$ pip install jsonfmt

Usage

$ jsonfmt [options] [files ...]
  • Positional arguments:

    • files: the files that will be processed
  • Options:

    • -h, --help: show this help message and exit
    • -c: compact the json object to a single line
    • -C: copy the result to clipboard
    • -e: escape non-ASCII characters
    • -f {json,toml,yaml}: the format to output (default: json)
    • -i INDENT: number of spaces for indentation (default: 2)
    • -O: overwrite the formated text to original file
    • -p JSONPATH: output part of the object via jsonpath
    • -v: show the version

Example

There are some test data in folder test:

test/
|- example.json
|- example.toml
|- example.yaml

1. Pretty print JSON object.

  • read from file

    # format the json with 4-space indentaion
    $ jsonfmt -i 4 test/example.json
    

    Output:

    {
        "actions": [
            {
                "calorie": 294.9,
                "date": "2021-03-02",
                "name": "eat"
            },
            {
                "calorie": -375,
                "date": "2023-04-27",
                "name": "sport"
            }
        ],
        "age": 23,
        "gender": "纯爷们",
        "money": 3.1415926,
        "name": "Bob"
    }
    
  • read from stdin

    $ cat test/example.json | jsonfmt -i 4
    

    Output: Ditto.

2. Minimize the JSON object.

$ echo '{
    "name": "alex",
    "age": 21,
    "items": [
        "pen",
        "phone"
    ]
}' | jsonfmt -c

Output:

{"age":21,"items":["pen","phone"],"name":"alex"}

3. Pick out parts of a large JSON via JSONPath.

JSONPath is a way to query the sub-elements of a JSON document.

It likes the XPath for xml, which can extract part of the content of a given JSON document through a simple syntax.

JSONPath syntax reference https://goessner.net/articles/JsonPath/ and https://datatracker.ietf.org/doc/id/draft-goessner-dispatch-jsonpath-00.html

Some examples:

  • pick out the first actions in example.json

    $ jsonfmt -p '$.actions[0]' test/example.json
    

    Output:

    [
        {
            "calorie": 294.9,
            "date": "2021-03-02",
            "name": "eat"
        }
    ]
    
  • Filters all occurrences of the name field in the JSON.

    $ jsonfmt -p '$..name' test/example.json
    

    Output:

    [
        "Bob",
        "eat",
        "sport"
    ]
    

4. Format JSON as TOML or YAML.

$ jsonfmt test/example.json -f toml

Output:

age = 23
gender = "纯爷们"
money = 3.1415926
name = "Bob"
[[actions]]
calorie = 294.9
date = "2021-03-02"
name = "eat"

[[actions]]
calorie = -375
date = "2023-04-27"
name = "sport"

5. Conversion between JSON, TOML and YAML formats.

Note this:
The `null` value is invalid in TOML. Therefore, any null values in JSON or YAML will be removed when converting to TOML.
# json to yaml
$ jsonfmt test/example.json -f yaml

# yaml to toml
$ jsonfmt test/example.yaml -f toml

# toml to json
$ jsonfmt test/example.toml -f json

6. Copy the result to clipboard.

$ jsonfmt -C test/example.json

# Output
jsonfmt: result copied to clipboard.

Once you've done the above, you can then use ctrl+v or cmd+v to paste the result anywhere on your computer.

Note these:
  • When you specify the -C option, any output destination other than the clipboard will be ignored.
  • When you process multiple files, only the last result will be preserved in the clipboard.

7. Output to file.

  • use the -O parameter to overwrite the file with the result.
$ jsonfmt -O test/example.json
  • write the result to a new file (use symbol >).
$ jsonfmt test/example.json > formatted.json

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jsonfmt-0.2.3.tar.gz (8.6 kB view hashes)

Uploaded Source

Built Distribution

jsonfmt-0.2.3-py3-none-any.whl (6.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page