A simple tool for formatting JSON object.
Project description
JSON Formator
jsonfmt is a CLI tool for pretty printing or compressing JSON objects.
It has the following features:
- Print JSON with hightlight and pretty format from files or stdin.
- Minimize JSON to a single line.
- Pick out parts from a large JSON via jsonpath.
- Format JSON to TOML or YAML.
- Conversion between these three formats.
- 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.
# 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.
- 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.