json-formatter-validator-cli 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- json_formatter_validator_cli-0.1.0/LICENSE +21 -0
- json_formatter_validator_cli-0.1.0/PKG-INFO +288 -0
- json_formatter_validator_cli-0.1.0/README.md +267 -0
- json_formatter_validator_cli-0.1.0/pyproject.toml +31 -0
- json_formatter_validator_cli-0.1.0/setup.cfg +4 -0
- json_formatter_validator_cli-0.1.0/src/json_formatter_validator.py +229 -0
- json_formatter_validator_cli-0.1.0/src/json_formatter_validator_cli.egg-info/PKG-INFO +288 -0
- json_formatter_validator_cli-0.1.0/src/json_formatter_validator_cli.egg-info/SOURCES.txt +10 -0
- json_formatter_validator_cli-0.1.0/src/json_formatter_validator_cli.egg-info/dependency_links.txt +1 -0
- json_formatter_validator_cli-0.1.0/src/json_formatter_validator_cli.egg-info/entry_points.txt +2 -0
- json_formatter_validator_cli-0.1.0/src/json_formatter_validator_cli.egg-info/top_level.txt +1 -0
- json_formatter_validator_cli-0.1.0/tests/test_json_formatter_validator.py +321 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Haider Ali
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: json-formatter-validator-cli
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Format and validate JSON, with line:col errors, fail CI on bad JSON.
|
|
5
|
+
Author: Haider Ali
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/haiderstudio/json-formatter-validator-cli
|
|
8
|
+
Project-URL: Repository, https://github.com/haiderstudio/json-formatter-validator-cli
|
|
9
|
+
Project-URL: Issues, https://github.com/haiderstudio/json-formatter-validator-cli/issues
|
|
10
|
+
Keywords: json,formatter,validator,cli,developer-tools
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Environment :: Console
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Topic :: Software Development
|
|
17
|
+
Requires-Python: >=3.9
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
License-File: LICENSE
|
|
20
|
+
Dynamic: license-file
|
|
21
|
+
|
|
22
|
+
# json-formatter-validator-cli
|
|
23
|
+
|
|
24
|
+
Format and validate JSON, with line:col errors, fail CI on bad JSON.
|
|
25
|
+
|
|
26
|
+
[](https://github.com/haiderstudio/json-formatter-validator-cli/actions/workflows/ci.yml) [](LICENSE)
|
|
27
|
+
|
|
28
|
+
## What it does
|
|
29
|
+
|
|
30
|
+
`json_formatter_validator` reads a JSON file (or standard input),
|
|
31
|
+
pretty-formats it, and validates it. A bad file is reported with the
|
|
32
|
+
exact line and column of the error, plus the offending line with a
|
|
33
|
+
caret under the bad spot — so you can find the mistake without
|
|
34
|
+
opening an editor and hunting for it.
|
|
35
|
+
|
|
36
|
+
- It formats one file to standard output by default (2-space indent).
|
|
37
|
+
`--indent N` changes the indent, `--sort-keys` sorts object keys,
|
|
38
|
+
`--minify` writes compact JSON with no extra spaces or newlines.
|
|
39
|
+
- It reads from standard input when no file is named, so it works in
|
|
40
|
+
a pipe.
|
|
41
|
+
- `--check` only validates: it prints `VALID` or `INVALID` per file
|
|
42
|
+
and checks as many files as you name. It exits 1 when any file has
|
|
43
|
+
bad JSON, so it can fail a CI run.
|
|
44
|
+
- It never rewrites a file unless you ask: `--in-place` rewrites the
|
|
45
|
+
input file, `--output FILE` writes the formatted text to the file
|
|
46
|
+
you name. A bad file is never rewritten — it is reported and left
|
|
47
|
+
alone.
|
|
48
|
+
- It is report-only and runs fully offline: it sends nothing anywhere
|
|
49
|
+
and needs no API key.
|
|
50
|
+
|
|
51
|
+
## What it does NOT do
|
|
52
|
+
|
|
53
|
+
- It does not check JSON against a schema. It checks syntax only: can
|
|
54
|
+
a parser read this text, and where exactly does it break.
|
|
55
|
+
- It does not fix bad JSON. A missing comma is reported with its line
|
|
56
|
+
and column, not repaired.
|
|
57
|
+
- It does not check duplicate keys, and it does not warn about them.
|
|
58
|
+
Two keys with the same name are read the way Python's `json` module
|
|
59
|
+
reads them (the last one wins).
|
|
60
|
+
- It does not query, filter, or transform JSON. If you need to pick
|
|
61
|
+
fields out of a document, that is a different tool's job.
|
|
62
|
+
- It does not accept comments, trailing commas, or single quotes.
|
|
63
|
+
Those are not JSON, and they are reported as errors.
|
|
64
|
+
|
|
65
|
+
## Who it is for
|
|
66
|
+
|
|
67
|
+
Anyone who keeps JSON files around and wants a quick, scriptable
|
|
68
|
+
check that they are well-formed: a developer adding a CI step that
|
|
69
|
+
fails the build on a broken config, someone hand-editing a settings
|
|
70
|
+
file, or anyone piping API output through a formatter to read it. If
|
|
71
|
+
the file is valid, you get clean formatted JSON; if it is not, the
|
|
72
|
+
report says exactly which line and column broke.
|
|
73
|
+
|
|
74
|
+
## How this differs from similar tools
|
|
75
|
+
|
|
76
|
+
JSON formatters already exist, and most of them are web pages or
|
|
77
|
+
editors: JSONLint and jsonformatter.org format in a browser, and the
|
|
78
|
+
top GitHub hit for the exact name, mariazevedo88/json-formatter-validator
|
|
79
|
+
(12 stars), is a Java tool. The big command-line name in this space
|
|
80
|
+
is jq, but jq is a query and transform language — formatting is a
|
|
81
|
+
side effect of it, not its job, and it needs a separate install.
|
|
82
|
+
Python already ships `python -m json.tool`, which pretty-prints and
|
|
83
|
+
exits nonzero on bad JSON, but it reports only the error message: no
|
|
84
|
+
line and column pointer, no `--check` mode for several files at once,
|
|
85
|
+
and no `--in-place` or `--output` for writing formatted files.
|
|
86
|
+
Schema validators such as the ajv CLI or jsonvalidator-cli check JSON
|
|
87
|
+
against a schema, which is a different job from checking syntax.
|
|
88
|
+
|
|
89
|
+
This tool does a narrower job: a Python standard-library-only CLI
|
|
90
|
+
that formats one file or checks many, reports every error with a
|
|
91
|
+
line, a column, and a caret, and exits 1 on bad JSON for CI — no
|
|
92
|
+
install beyond Python, no browser, no schema, no query language.
|
|
93
|
+
|
|
94
|
+
## Install
|
|
95
|
+
|
|
96
|
+
Python standard library only — no packages to download. Clone the
|
|
97
|
+
repo and run it in place:
|
|
98
|
+
|
|
99
|
+
```sh
|
|
100
|
+
git clone https://github.com/haiderstudio/json-formatter-validator-cli.git
|
|
101
|
+
cd json-formatter-validator-cli
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
Python 3.10 or newer.
|
|
105
|
+
|
|
106
|
+
## Usage
|
|
107
|
+
|
|
108
|
+
Format a file:
|
|
109
|
+
|
|
110
|
+
```sh
|
|
111
|
+
python3 src/json_formatter_validator.py examples/compact.json
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
Real output:
|
|
115
|
+
|
|
116
|
+
```text
|
|
117
|
+
{
|
|
118
|
+
"name": "example-site",
|
|
119
|
+
"pages": [
|
|
120
|
+
{
|
|
121
|
+
"draft": false,
|
|
122
|
+
"path": "/",
|
|
123
|
+
"title": "Home"
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
"draft": false,
|
|
127
|
+
"path": "/about",
|
|
128
|
+
"title": "About"
|
|
129
|
+
}
|
|
130
|
+
],
|
|
131
|
+
"private": false,
|
|
132
|
+
"settings": {
|
|
133
|
+
"postsPerPage": 10,
|
|
134
|
+
"theme": "light"
|
|
135
|
+
},
|
|
136
|
+
"version": "1.4.0"
|
|
137
|
+
}
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
The command exits 0. The input file is left alone.
|
|
141
|
+
|
|
142
|
+
Check several files at once:
|
|
143
|
+
|
|
144
|
+
```sh
|
|
145
|
+
python3 src/json_formatter_validator.py --check examples/valid.json examples/compact.json examples/invalid-missing-comma.json examples/invalid-trailing-comma.json examples/invalid-single-quotes.json
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
Real output:
|
|
149
|
+
|
|
150
|
+
```text
|
|
151
|
+
VALID examples/valid.json
|
|
152
|
+
VALID examples/compact.json
|
|
153
|
+
INVALID examples/invalid-missing-comma.json:4:3: Expecting ',' delimiter
|
|
154
|
+
"private": false
|
|
155
|
+
^
|
|
156
|
+
INVALID examples/invalid-trailing-comma.json:4:1: Expecting property name enclosed in double quotes
|
|
157
|
+
}
|
|
158
|
+
^
|
|
159
|
+
INVALID examples/invalid-single-quotes.json:2:3: Expecting property name enclosed in double quotes
|
|
160
|
+
'name': 'example-site',
|
|
161
|
+
^
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
The command exits 1, because three files are bad. The same output
|
|
165
|
+
is saved in `examples/sample-output.txt`.
|
|
166
|
+
|
|
167
|
+
Format from a pipe, with sorted keys:
|
|
168
|
+
|
|
169
|
+
```sh
|
|
170
|
+
echo '{"b":1,"a":2}' | python3 src/json_formatter_validator.py --sort-keys
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
Real output:
|
|
174
|
+
|
|
175
|
+
```text
|
|
176
|
+
{
|
|
177
|
+
"a": 2,
|
|
178
|
+
"b": 1
|
|
179
|
+
}
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
Minify a file:
|
|
183
|
+
|
|
184
|
+
```sh
|
|
185
|
+
python3 src/json_formatter_validator.py --minify examples/valid.json
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
Real output:
|
|
189
|
+
|
|
190
|
+
```text
|
|
191
|
+
{"name":"example-site","pages":[{"draft":false,"path":"/","title":"Home"},{"draft":false,"path":"/about","title":"About"}],"private":false,"settings":{"postsPerPage":10,"theme":"light"},"version":"1.4.0"}
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
## Examples
|
|
195
|
+
|
|
196
|
+
### 1. Fail CI on a bad config file
|
|
197
|
+
|
|
198
|
+
```sh
|
|
199
|
+
python3 src/json_formatter_validator.py --check config.json
|
|
200
|
+
echo $?
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
```text
|
|
204
|
+
1
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
The exit code is 1 when the file has bad JSON. Put the same command
|
|
208
|
+
in CI and the build fails until the file is fixed. With a valid file
|
|
209
|
+
the same command prints `VALID config.json` and exits 0.
|
|
210
|
+
|
|
211
|
+
### 2. A missing comma, found by line and column
|
|
212
|
+
|
|
213
|
+
`examples/invalid-missing-comma.json` is missing the comma after
|
|
214
|
+
`"version": "1.4.0"`:
|
|
215
|
+
|
|
216
|
+
```sh
|
|
217
|
+
python3 src/json_formatter_validator.py --check examples/invalid-missing-comma.json
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
```text
|
|
221
|
+
INVALID examples/invalid-missing-comma.json:4:3: Expecting ',' delimiter
|
|
222
|
+
"private": false
|
|
223
|
+
^
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
Line 4, column 3 — the start of `"private"` — is where the parser
|
|
227
|
+
expected the comma. The caret points at it.
|
|
228
|
+
|
|
229
|
+
### 3. Rewrite a file in place, only when asked
|
|
230
|
+
|
|
231
|
+
```sh
|
|
232
|
+
python3 src/json_formatter_validator.py --in-place examples/compact.json
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
```text
|
|
236
|
+
formatted examples/compact.json
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
The file is now pretty-printed. Without `--in-place` (or
|
|
240
|
+
`--output`), the same command prints the formatted text and leaves
|
|
241
|
+
the file alone. A bad file is never rewritten, even with
|
|
242
|
+
`--in-place`.
|
|
243
|
+
|
|
244
|
+
## How it is tested
|
|
245
|
+
|
|
246
|
+
```sh
|
|
247
|
+
python3 tests/test_json_formatter_validator.py
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
35 tests, standard library `unittest` only — no third-party
|
|
251
|
+
packages. They cover: formatting (default indent, `--indent 4`,
|
|
252
|
+
indent 0, `--sort-keys`, `--minify`, non-ASCII escaping), error
|
|
253
|
+
positions (missing comma at 4:3, trailing data at 1:9, empty input
|
|
254
|
+
at 1:1, trailing comma, single quotes), the `--check` mode (valid
|
|
255
|
+
and invalid mixed, all valid, stdin, pointer lines), the write modes
|
|
256
|
+
(`--in-place` rewrites and reports, bad files are never rewritten,
|
|
257
|
+
`--output` writes a named file, plain formatting never changes the
|
|
258
|
+
input), the usage errors (missing file, bad indent, `--output` with
|
|
259
|
+
two files, `--in-place` with stdin), the bundled examples (the
|
|
260
|
+
committed `examples/sample-output.txt` is compared against a fresh
|
|
261
|
+
run, and the README's quoted outputs are checked against it), the
|
|
262
|
+
example script, and a check that the code and the tests import
|
|
263
|
+
standard library only.
|
|
264
|
+
|
|
265
|
+
The bundled example is a real run, not made-up output:
|
|
266
|
+
|
|
267
|
+
```sh
|
|
268
|
+
sh examples/run-example.sh
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
It formats `examples/compact.json`, checks all five example files,
|
|
272
|
+
and formats one line from stdin. The committed
|
|
273
|
+
`examples/sample-output.txt` is the output of the check run.
|
|
274
|
+
|
|
275
|
+
## Contributing
|
|
276
|
+
|
|
277
|
+
Small, focused improvements are welcome — see
|
|
278
|
+
[CONTRIBUTING.md](CONTRIBUTING.md).
|
|
279
|
+
|
|
280
|
+
## Licence
|
|
281
|
+
|
|
282
|
+
MIT — see [LICENSE](LICENSE).
|
|
283
|
+
|
|
284
|
+
## Original work
|
|
285
|
+
|
|
286
|
+
Built from scratch for this repo. The JSON parsing and error
|
|
287
|
+
positions come from Python's standard `json` module; no code was
|
|
288
|
+
copied from any existing formatter or validator.
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
# json-formatter-validator-cli
|
|
2
|
+
|
|
3
|
+
Format and validate JSON, with line:col errors, fail CI on bad JSON.
|
|
4
|
+
|
|
5
|
+
[](https://github.com/haiderstudio/json-formatter-validator-cli/actions/workflows/ci.yml) [](LICENSE)
|
|
6
|
+
|
|
7
|
+
## What it does
|
|
8
|
+
|
|
9
|
+
`json_formatter_validator` reads a JSON file (or standard input),
|
|
10
|
+
pretty-formats it, and validates it. A bad file is reported with the
|
|
11
|
+
exact line and column of the error, plus the offending line with a
|
|
12
|
+
caret under the bad spot — so you can find the mistake without
|
|
13
|
+
opening an editor and hunting for it.
|
|
14
|
+
|
|
15
|
+
- It formats one file to standard output by default (2-space indent).
|
|
16
|
+
`--indent N` changes the indent, `--sort-keys` sorts object keys,
|
|
17
|
+
`--minify` writes compact JSON with no extra spaces or newlines.
|
|
18
|
+
- It reads from standard input when no file is named, so it works in
|
|
19
|
+
a pipe.
|
|
20
|
+
- `--check` only validates: it prints `VALID` or `INVALID` per file
|
|
21
|
+
and checks as many files as you name. It exits 1 when any file has
|
|
22
|
+
bad JSON, so it can fail a CI run.
|
|
23
|
+
- It never rewrites a file unless you ask: `--in-place` rewrites the
|
|
24
|
+
input file, `--output FILE` writes the formatted text to the file
|
|
25
|
+
you name. A bad file is never rewritten — it is reported and left
|
|
26
|
+
alone.
|
|
27
|
+
- It is report-only and runs fully offline: it sends nothing anywhere
|
|
28
|
+
and needs no API key.
|
|
29
|
+
|
|
30
|
+
## What it does NOT do
|
|
31
|
+
|
|
32
|
+
- It does not check JSON against a schema. It checks syntax only: can
|
|
33
|
+
a parser read this text, and where exactly does it break.
|
|
34
|
+
- It does not fix bad JSON. A missing comma is reported with its line
|
|
35
|
+
and column, not repaired.
|
|
36
|
+
- It does not check duplicate keys, and it does not warn about them.
|
|
37
|
+
Two keys with the same name are read the way Python's `json` module
|
|
38
|
+
reads them (the last one wins).
|
|
39
|
+
- It does not query, filter, or transform JSON. If you need to pick
|
|
40
|
+
fields out of a document, that is a different tool's job.
|
|
41
|
+
- It does not accept comments, trailing commas, or single quotes.
|
|
42
|
+
Those are not JSON, and they are reported as errors.
|
|
43
|
+
|
|
44
|
+
## Who it is for
|
|
45
|
+
|
|
46
|
+
Anyone who keeps JSON files around and wants a quick, scriptable
|
|
47
|
+
check that they are well-formed: a developer adding a CI step that
|
|
48
|
+
fails the build on a broken config, someone hand-editing a settings
|
|
49
|
+
file, or anyone piping API output through a formatter to read it. If
|
|
50
|
+
the file is valid, you get clean formatted JSON; if it is not, the
|
|
51
|
+
report says exactly which line and column broke.
|
|
52
|
+
|
|
53
|
+
## How this differs from similar tools
|
|
54
|
+
|
|
55
|
+
JSON formatters already exist, and most of them are web pages or
|
|
56
|
+
editors: JSONLint and jsonformatter.org format in a browser, and the
|
|
57
|
+
top GitHub hit for the exact name, mariazevedo88/json-formatter-validator
|
|
58
|
+
(12 stars), is a Java tool. The big command-line name in this space
|
|
59
|
+
is jq, but jq is a query and transform language — formatting is a
|
|
60
|
+
side effect of it, not its job, and it needs a separate install.
|
|
61
|
+
Python already ships `python -m json.tool`, which pretty-prints and
|
|
62
|
+
exits nonzero on bad JSON, but it reports only the error message: no
|
|
63
|
+
line and column pointer, no `--check` mode for several files at once,
|
|
64
|
+
and no `--in-place` or `--output` for writing formatted files.
|
|
65
|
+
Schema validators such as the ajv CLI or jsonvalidator-cli check JSON
|
|
66
|
+
against a schema, which is a different job from checking syntax.
|
|
67
|
+
|
|
68
|
+
This tool does a narrower job: a Python standard-library-only CLI
|
|
69
|
+
that formats one file or checks many, reports every error with a
|
|
70
|
+
line, a column, and a caret, and exits 1 on bad JSON for CI — no
|
|
71
|
+
install beyond Python, no browser, no schema, no query language.
|
|
72
|
+
|
|
73
|
+
## Install
|
|
74
|
+
|
|
75
|
+
Python standard library only — no packages to download. Clone the
|
|
76
|
+
repo and run it in place:
|
|
77
|
+
|
|
78
|
+
```sh
|
|
79
|
+
git clone https://github.com/haiderstudio/json-formatter-validator-cli.git
|
|
80
|
+
cd json-formatter-validator-cli
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Python 3.10 or newer.
|
|
84
|
+
|
|
85
|
+
## Usage
|
|
86
|
+
|
|
87
|
+
Format a file:
|
|
88
|
+
|
|
89
|
+
```sh
|
|
90
|
+
python3 src/json_formatter_validator.py examples/compact.json
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Real output:
|
|
94
|
+
|
|
95
|
+
```text
|
|
96
|
+
{
|
|
97
|
+
"name": "example-site",
|
|
98
|
+
"pages": [
|
|
99
|
+
{
|
|
100
|
+
"draft": false,
|
|
101
|
+
"path": "/",
|
|
102
|
+
"title": "Home"
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
"draft": false,
|
|
106
|
+
"path": "/about",
|
|
107
|
+
"title": "About"
|
|
108
|
+
}
|
|
109
|
+
],
|
|
110
|
+
"private": false,
|
|
111
|
+
"settings": {
|
|
112
|
+
"postsPerPage": 10,
|
|
113
|
+
"theme": "light"
|
|
114
|
+
},
|
|
115
|
+
"version": "1.4.0"
|
|
116
|
+
}
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
The command exits 0. The input file is left alone.
|
|
120
|
+
|
|
121
|
+
Check several files at once:
|
|
122
|
+
|
|
123
|
+
```sh
|
|
124
|
+
python3 src/json_formatter_validator.py --check examples/valid.json examples/compact.json examples/invalid-missing-comma.json examples/invalid-trailing-comma.json examples/invalid-single-quotes.json
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
Real output:
|
|
128
|
+
|
|
129
|
+
```text
|
|
130
|
+
VALID examples/valid.json
|
|
131
|
+
VALID examples/compact.json
|
|
132
|
+
INVALID examples/invalid-missing-comma.json:4:3: Expecting ',' delimiter
|
|
133
|
+
"private": false
|
|
134
|
+
^
|
|
135
|
+
INVALID examples/invalid-trailing-comma.json:4:1: Expecting property name enclosed in double quotes
|
|
136
|
+
}
|
|
137
|
+
^
|
|
138
|
+
INVALID examples/invalid-single-quotes.json:2:3: Expecting property name enclosed in double quotes
|
|
139
|
+
'name': 'example-site',
|
|
140
|
+
^
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
The command exits 1, because three files are bad. The same output
|
|
144
|
+
is saved in `examples/sample-output.txt`.
|
|
145
|
+
|
|
146
|
+
Format from a pipe, with sorted keys:
|
|
147
|
+
|
|
148
|
+
```sh
|
|
149
|
+
echo '{"b":1,"a":2}' | python3 src/json_formatter_validator.py --sort-keys
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
Real output:
|
|
153
|
+
|
|
154
|
+
```text
|
|
155
|
+
{
|
|
156
|
+
"a": 2,
|
|
157
|
+
"b": 1
|
|
158
|
+
}
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
Minify a file:
|
|
162
|
+
|
|
163
|
+
```sh
|
|
164
|
+
python3 src/json_formatter_validator.py --minify examples/valid.json
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
Real output:
|
|
168
|
+
|
|
169
|
+
```text
|
|
170
|
+
{"name":"example-site","pages":[{"draft":false,"path":"/","title":"Home"},{"draft":false,"path":"/about","title":"About"}],"private":false,"settings":{"postsPerPage":10,"theme":"light"},"version":"1.4.0"}
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
## Examples
|
|
174
|
+
|
|
175
|
+
### 1. Fail CI on a bad config file
|
|
176
|
+
|
|
177
|
+
```sh
|
|
178
|
+
python3 src/json_formatter_validator.py --check config.json
|
|
179
|
+
echo $?
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
```text
|
|
183
|
+
1
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
The exit code is 1 when the file has bad JSON. Put the same command
|
|
187
|
+
in CI and the build fails until the file is fixed. With a valid file
|
|
188
|
+
the same command prints `VALID config.json` and exits 0.
|
|
189
|
+
|
|
190
|
+
### 2. A missing comma, found by line and column
|
|
191
|
+
|
|
192
|
+
`examples/invalid-missing-comma.json` is missing the comma after
|
|
193
|
+
`"version": "1.4.0"`:
|
|
194
|
+
|
|
195
|
+
```sh
|
|
196
|
+
python3 src/json_formatter_validator.py --check examples/invalid-missing-comma.json
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
```text
|
|
200
|
+
INVALID examples/invalid-missing-comma.json:4:3: Expecting ',' delimiter
|
|
201
|
+
"private": false
|
|
202
|
+
^
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
Line 4, column 3 — the start of `"private"` — is where the parser
|
|
206
|
+
expected the comma. The caret points at it.
|
|
207
|
+
|
|
208
|
+
### 3. Rewrite a file in place, only when asked
|
|
209
|
+
|
|
210
|
+
```sh
|
|
211
|
+
python3 src/json_formatter_validator.py --in-place examples/compact.json
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
```text
|
|
215
|
+
formatted examples/compact.json
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
The file is now pretty-printed. Without `--in-place` (or
|
|
219
|
+
`--output`), the same command prints the formatted text and leaves
|
|
220
|
+
the file alone. A bad file is never rewritten, even with
|
|
221
|
+
`--in-place`.
|
|
222
|
+
|
|
223
|
+
## How it is tested
|
|
224
|
+
|
|
225
|
+
```sh
|
|
226
|
+
python3 tests/test_json_formatter_validator.py
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
35 tests, standard library `unittest` only — no third-party
|
|
230
|
+
packages. They cover: formatting (default indent, `--indent 4`,
|
|
231
|
+
indent 0, `--sort-keys`, `--minify`, non-ASCII escaping), error
|
|
232
|
+
positions (missing comma at 4:3, trailing data at 1:9, empty input
|
|
233
|
+
at 1:1, trailing comma, single quotes), the `--check` mode (valid
|
|
234
|
+
and invalid mixed, all valid, stdin, pointer lines), the write modes
|
|
235
|
+
(`--in-place` rewrites and reports, bad files are never rewritten,
|
|
236
|
+
`--output` writes a named file, plain formatting never changes the
|
|
237
|
+
input), the usage errors (missing file, bad indent, `--output` with
|
|
238
|
+
two files, `--in-place` with stdin), the bundled examples (the
|
|
239
|
+
committed `examples/sample-output.txt` is compared against a fresh
|
|
240
|
+
run, and the README's quoted outputs are checked against it), the
|
|
241
|
+
example script, and a check that the code and the tests import
|
|
242
|
+
standard library only.
|
|
243
|
+
|
|
244
|
+
The bundled example is a real run, not made-up output:
|
|
245
|
+
|
|
246
|
+
```sh
|
|
247
|
+
sh examples/run-example.sh
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
It formats `examples/compact.json`, checks all five example files,
|
|
251
|
+
and formats one line from stdin. The committed
|
|
252
|
+
`examples/sample-output.txt` is the output of the check run.
|
|
253
|
+
|
|
254
|
+
## Contributing
|
|
255
|
+
|
|
256
|
+
Small, focused improvements are welcome — see
|
|
257
|
+
[CONTRIBUTING.md](CONTRIBUTING.md).
|
|
258
|
+
|
|
259
|
+
## Licence
|
|
260
|
+
|
|
261
|
+
MIT — see [LICENSE](LICENSE).
|
|
262
|
+
|
|
263
|
+
## Original work
|
|
264
|
+
|
|
265
|
+
Built from scratch for this repo. The JSON parsing and error
|
|
266
|
+
positions come from Python's standard `json` module; no code was
|
|
267
|
+
copied from any existing formatter or validator.
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "json-formatter-validator-cli"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Format and validate JSON, with line:col errors, fail CI on bad JSON."
|
|
9
|
+
readme = { file = "README.md", content-type = "text/markdown" }
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
authors = [{ name = "Haider Ali" }]
|
|
13
|
+
keywords = ["json", "formatter", "validator", "cli", "developer-tools"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 4 - Beta",
|
|
16
|
+
"Environment :: Console",
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"License :: OSI Approved :: MIT License",
|
|
19
|
+
"Programming Language :: Python :: 3",
|
|
20
|
+
"Topic :: Software Development",
|
|
21
|
+
]
|
|
22
|
+
urls = { Homepage = "https://github.com/haiderstudio/json-formatter-validator-cli", Repository = "https://github.com/haiderstudio/json-formatter-validator-cli", Issues = "https://github.com/haiderstudio/json-formatter-validator-cli/issues" }
|
|
23
|
+
|
|
24
|
+
[project.scripts]
|
|
25
|
+
json-formatter-validator = "json_formatter_validator:main"
|
|
26
|
+
|
|
27
|
+
[tool.setuptools.package-dir]
|
|
28
|
+
"" = "src"
|
|
29
|
+
|
|
30
|
+
[tool.setuptools]
|
|
31
|
+
py-modules = ["json_formatter_validator"]
|