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
namefield 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
-Coption, 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
-Oparameter 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file jsonfmt-0.2.3.tar.gz.
File metadata
- Download URL: jsonfmt-0.2.3.tar.gz
- Upload date:
- Size: 8.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2230c6d663a01ce5f1954a50eec320b13b21d9108df128b888a388e1e6c495bd
|
|
| MD5 |
cb663fcd95df6dd7a379fc14ed0bf84c
|
|
| BLAKE2b-256 |
3cf362629b0b45159b45aa66bb59737598891b38651f8f69cb87b6810171af40
|
File details
Details for the file jsonfmt-0.2.3-py3-none-any.whl.
File metadata
- Download URL: jsonfmt-0.2.3-py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
76dd5c4fbc52ae290c013900346c095cafc7882160451c0f7c24c44565f57d76
|
|
| MD5 |
4a5981738927ae0578c1e9a2870145ce
|
|
| BLAKE2b-256 |
f289698238f2be4b9728bb386fceae0c97eeb0debb0077c68b94c58ab0de4b4b
|