devkits-json-formatter 1.0.1 → 1.0.4
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.
- package/README.md +38 -104
- package/index.js +69 -153
- package/package.json +30 -17
package/README.md
CHANGED
|
@@ -1,125 +1,59 @@
|
|
|
1
|
-
#
|
|
1
|
+
# devkits-json-formatter
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Format and validate JSON from CLI or stdin. Part of [DevKits Tools](https://devkits-tools.surge.sh).
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
## 🧰 DevKits Pro Tools
|
|
8
|
-
|
|
9
|
-
This package is part of **DevKits** — 82+ free developer tools. The online JSON Formatter includes schema validation, tree view, and query/filter tools.
|
|
10
|
-
|
|
11
|
-
**→ [Format JSON with Tree View](https://devkits-tools.surge.sh)**
|
|
12
|
-
|
|
13
|
-
## Installation
|
|
5
|
+
## Install
|
|
14
6
|
|
|
15
7
|
```bash
|
|
16
|
-
npm install
|
|
8
|
+
npm install -g devkits-json-formatter
|
|
17
9
|
```
|
|
18
10
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
```javascript
|
|
22
|
-
const {
|
|
23
|
-
format,
|
|
24
|
-
minify,
|
|
25
|
-
isValid,
|
|
26
|
-
pretty,
|
|
27
|
-
countKeys,
|
|
28
|
-
getSizeInfo,
|
|
29
|
-
sortKeys,
|
|
30
|
-
formatSorted
|
|
31
|
-
} = require('@devkits/json-formatter');
|
|
32
|
-
|
|
33
|
-
const uglyJson = '{"name":"John","age":30,"city":"NYC"}';
|
|
34
|
-
|
|
35
|
-
// Format JSON with indentation
|
|
36
|
-
const result = format(uglyJson, { indent: 2 });
|
|
37
|
-
// {
|
|
38
|
-
// success: true,
|
|
39
|
-
// result: '{\n "name": "John",\n "age": 30,\n "city": "NYC"\n}'
|
|
40
|
-
// }
|
|
41
|
-
|
|
42
|
-
// Minify JSON
|
|
43
|
-
const min = minify(uglyJson);
|
|
44
|
-
// { success: true, result: '{"name":"John",...}', saved: 15 }
|
|
45
|
-
|
|
46
|
-
// Validate JSON
|
|
47
|
-
isValid('{"valid": true}');
|
|
48
|
-
// { valid: true }
|
|
49
|
-
|
|
50
|
-
isValid('{invalid}');
|
|
51
|
-
// { valid: false, error: 'Unexpected token...', position: {line: 1, column: 2} }
|
|
52
|
-
|
|
53
|
-
// Pretty print an object
|
|
54
|
-
pretty({ name: 'John', age: 30 });
|
|
55
|
-
// '{\n "name": "John",\n "age": 30\n}'
|
|
56
|
-
|
|
57
|
-
// Count keys recursively
|
|
58
|
-
const nested = { a: { b: { c: 1 } }, d: [1, 2, 3] };
|
|
59
|
-
countKeys(nested);
|
|
60
|
-
// 5
|
|
61
|
-
|
|
62
|
-
// Get size info
|
|
63
|
-
getSizeInfo(uglyJson);
|
|
64
|
-
// { original: 36, minified: 36, saved: 0, keys: 3 }
|
|
65
|
-
|
|
66
|
-
// Sort keys alphabetically
|
|
67
|
-
const unsorted = { z: 1, a: 2, m: 3 };
|
|
68
|
-
sortKeys(unsorted);
|
|
69
|
-
// { a: 2, m: 3, z: 1 }
|
|
70
|
-
|
|
71
|
-
// Format with sorted keys
|
|
72
|
-
formatSorted('{"z":1,"a":2}', 2);
|
|
73
|
-
// { success: true, result: '{\n "a": 2,\n "z": 1\n}' }
|
|
74
|
-
```
|
|
75
|
-
|
|
76
|
-
## API
|
|
77
|
-
|
|
78
|
-
### `format(jsonString, options)`
|
|
79
|
-
Formats JSON with specified indentation (default: 2 spaces).
|
|
11
|
+
[](https://www.npmjs.com/package/devkits-json-formatter)
|
|
80
12
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
### `minify(jsonString)`
|
|
84
|
-
Removes all whitespace from JSON.
|
|
85
|
-
|
|
86
|
-
Returns `{success, result?, error?, saved?}`.
|
|
87
|
-
|
|
88
|
-
### `isValid(jsonString)`
|
|
89
|
-
Validates JSON syntax with optional error position.
|
|
13
|
+
## Usage
|
|
90
14
|
|
|
91
|
-
|
|
15
|
+
### Format JSON (pretty print)
|
|
92
16
|
|
|
93
|
-
|
|
94
|
-
|
|
17
|
+
```bash
|
|
18
|
+
# From stdin
|
|
19
|
+
echo '{"name":"DevKits","tools":84}' | jsonfmt
|
|
95
20
|
|
|
96
|
-
|
|
97
|
-
|
|
21
|
+
# From file
|
|
22
|
+
jsonfmt package.json
|
|
98
23
|
|
|
99
|
-
|
|
100
|
-
|
|
24
|
+
# From JSON string
|
|
25
|
+
jsonfmt '{"test":true}'
|
|
26
|
+
```
|
|
101
27
|
|
|
102
|
-
###
|
|
103
|
-
Returns new object with keys sorted alphabetically (recursive).
|
|
28
|
+
### Minify JSON
|
|
104
29
|
|
|
105
|
-
|
|
106
|
-
|
|
30
|
+
```bash
|
|
31
|
+
jsonfmt package.json --minify
|
|
32
|
+
jsonfmt '{"test":true}' -m
|
|
33
|
+
```
|
|
107
34
|
|
|
108
|
-
##
|
|
35
|
+
## 🚀 More Free Tools from DevKits
|
|
109
36
|
|
|
110
|
-
|
|
37
|
+
This CLI is part of [DevKits](https://devkits-tools.surge.sh?utm_source=npm&utm_medium=readme&utm_campaign=json-formatter) — 82 free developer tools + 20 Pro tools ($9).
|
|
111
38
|
|
|
112
|
-
###
|
|
39
|
+
### Free Tools
|
|
40
|
+
- **JSON Tools**: [Formatter/Validator](https://devkits-tools.surge.sh/tools/json-formatter?utm_source=npm&utm_medium=readme&utm_campaign=json-formatter) | [JSON → CSV/XML/Types](https://devkits-tools.surge.sh/tools/json-to-csv?utm_source=npm&utm_medium=readme&utm_campaign=json-formatter)
|
|
41
|
+
- **Encoding**: [Base64](https://devkits-tools.surge.sh/tools/base64?utm_source=npm&utm_medium=readme&utm_campaign=json-formatter) | [URL Encode/Decode](https://devkits-tools.surge.sh/tools/url-encode?utm_source=npm&utm_medium=readme&utm_campaign=json-formatter)
|
|
42
|
+
- **Crypto**: [Hash Generator](https://devkits-tools.surge.sh/tools/hash-generator?utm_source=npm&utm_medium=readme&utm_campaign=json-formatter) | [JWT Decoder](https://devkits-tools.surge.sh/tools/jwt-decoder?utm_source=npm&utm_medium=readme&utm_campaign=json-formatter)
|
|
43
|
+
- **Color**: [HEX/RGB/HSL Converter](https://devkits-tools.surge.sh/tools/color-converter?utm_source=npm&utm_medium=readme&utm_campaign=json-formatter)
|
|
44
|
+
- **CSS**: [Minifier/Formatter](https://devkits-tools.surge.sh/tools/css-tools?utm_source=npm&utm_medium=readme&utm_campaign=json-formatter)
|
|
113
45
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
| @devkits/csv-json-converter | Convert between CSV and JSON |
|
|
118
|
-
| @devkits/markdown-tools | Markdown utilities |
|
|
119
|
-
| @devkits/env-editor | .env file utilities |
|
|
46
|
+
### 👉 Other Products
|
|
47
|
+
- **[Invoicely](https://invoicely-app.surge.sh?utm_source=npm&utm_medium=readme&utm_campaign=json-formatter)** — Free invoice generator (6 templates, PDF export)
|
|
48
|
+
- **[SnapOG](https://snapog.surge.sh?utm_source=npm&utm_medium=readme&utm_campaign=json-formatter)** — Free OG image generator (20+ templates)
|
|
120
49
|
|
|
121
|
-
|
|
50
|
+
### 💎 Pro Tools ($9 one-time)
|
|
51
|
+
Unlock 20 Pro tools at [DevKits Pro](https://devkits-tools.surge.sh/pro?utm_source=npm&utm_medium=readme&utm_campaign=json-formatter):
|
|
52
|
+
- SQL → Code (Go/TS/Python/Rust)
|
|
53
|
+
- Advanced Regex with visual groups
|
|
54
|
+
- Mock Data Generator (1000+ rows)
|
|
55
|
+
- And 16 more → [See all Pro tools](https://devkits-tools.surge.sh/pro?utm_source=npm&utm_medium=readme&utm_campaign=json-formatter)
|
|
122
56
|
|
|
123
57
|
## License
|
|
124
58
|
|
|
125
|
-
MIT
|
|
59
|
+
MIT — DevKits Team
|
package/index.js
CHANGED
|
@@ -1,177 +1,93 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
|
-
* @devkits/json-formatter
|
|
4
|
+
* @devkits/json-formatter — Format and validate JSON
|
|
3
5
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
+
* Usage:
|
|
7
|
+
* jsonfmt <file|json> [--minify]
|
|
8
|
+
* echo '{"foo":"bar"}' | jsonfmt
|
|
9
|
+
* jsonfmt package.json
|
|
6
10
|
*/
|
|
7
11
|
|
|
8
|
-
|
|
9
|
-
* Format JSON string with indentation
|
|
10
|
-
* @param {string} jsonString - JSON string to format
|
|
11
|
-
* @param {Object} options - Format options
|
|
12
|
-
* @param {number} options.indent - Indentation spaces (default: 2)
|
|
13
|
-
* @returns {{success: boolean, result?: string, error?: string}}
|
|
14
|
-
*/
|
|
15
|
-
function format(jsonString, options = {}) {
|
|
16
|
-
const { indent = 2 } = options;
|
|
12
|
+
const fs = require('fs');
|
|
17
13
|
|
|
18
|
-
|
|
19
|
-
const parsed = JSON.parse(jsonString);
|
|
20
|
-
return {
|
|
21
|
-
success: true,
|
|
22
|
-
result: JSON.stringify(parsed, null, indent)
|
|
23
|
-
};
|
|
24
|
-
} catch (e) {
|
|
25
|
-
return {
|
|
26
|
-
success: false,
|
|
27
|
-
error: e.message
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
}
|
|
14
|
+
const PRO_UPGRADE = '\n🚀 Want more? Upgrade to Pro: https://devkits-tools.surge.sh/pro\n Use code EARLYBIRD-2026 for 20% off\n';
|
|
31
15
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
16
|
+
function showHelp() {
|
|
17
|
+
console.log(`
|
|
18
|
+
@devkits/json-formatter — Format and validate JSON
|
|
19
|
+
|
|
20
|
+
Usage:
|
|
21
|
+
jsonfmt <file|json> [--minify]
|
|
22
|
+
jsonfmt package.json
|
|
23
|
+
jsonfmt '{"foo":"bar"}'
|
|
24
|
+
echo '{"test":true}' | jsonfmt
|
|
25
|
+
|
|
26
|
+
Options:
|
|
27
|
+
--minify, -m Minify JSON (remove whitespace)
|
|
28
|
+
--help, -h Show this help
|
|
29
|
+
|
|
30
|
+
Examples:
|
|
31
|
+
jsonfmt package.json
|
|
32
|
+
jsonfmt '{"foo":"bar"}' --minify
|
|
33
|
+
cat data.json | jsonfmt
|
|
34
|
+
|
|
35
|
+
Web Version: https://devkits-tools.surge.sh
|
|
36
|
+
Pro Features: https://devkits-tools.surge.sh/pro
|
|
37
|
+
`);
|
|
52
38
|
}
|
|
53
39
|
|
|
54
|
-
|
|
55
|
-
* Validate JSON syntax
|
|
56
|
-
* @param {string} jsonString - JSON string to validate
|
|
57
|
-
* @returns {{valid: boolean, error?: string, position?: {line: number, column: number}}}
|
|
58
|
-
*/
|
|
59
|
-
function isValid(jsonString) {
|
|
40
|
+
function formatJSON(input, minify = false) {
|
|
60
41
|
try {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
let position = null;
|
|
67
|
-
|
|
68
|
-
if (posMatch) {
|
|
69
|
-
const pos = parseInt(posMatch[1], 10);
|
|
70
|
-
const lines = jsonString.substring(0, pos).split('\n');
|
|
71
|
-
position = {
|
|
72
|
-
line: lines.length,
|
|
73
|
-
column: lines[lines.length - 1].length + 1
|
|
74
|
-
};
|
|
42
|
+
let jsonData;
|
|
43
|
+
|
|
44
|
+
// Check if input is a file path
|
|
45
|
+
if (fs.existsSync(input)) {
|
|
46
|
+
input = fs.readFileSync(input, 'utf-8');
|
|
75
47
|
}
|
|
76
48
|
|
|
77
|
-
|
|
78
|
-
valid: false,
|
|
79
|
-
error: e.message,
|
|
80
|
-
position
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
}
|
|
49
|
+
jsonData = JSON.parse(input);
|
|
84
50
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
function pretty(obj, indent = 2) {
|
|
92
|
-
return JSON.stringify(obj, null, indent);
|
|
93
|
-
}
|
|
51
|
+
if (minify) {
|
|
52
|
+
console.log(JSON.stringify(jsonData));
|
|
53
|
+
} else {
|
|
54
|
+
console.log(JSON.stringify(jsonData, null, 2));
|
|
55
|
+
console.log(`\n✅ Valid JSON`);
|
|
56
|
+
}
|
|
94
57
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
* @returns {number} Total key count
|
|
99
|
-
*/
|
|
100
|
-
function countKeys(obj) {
|
|
101
|
-
if (typeof obj !== 'object' || obj === null) return 0;
|
|
102
|
-
if (Array.isArray(obj)) {
|
|
103
|
-
return obj.reduce((n, v) => n + countKeys(v), 0);
|
|
58
|
+
} catch (err) {
|
|
59
|
+
console.error(`❌ Invalid JSON: ${err.message}`);
|
|
60
|
+
process.exit(1);
|
|
104
61
|
}
|
|
105
|
-
return Object.keys(obj).reduce((n, k) => n + 1 + countKeys(obj[k]), 0);
|
|
106
62
|
}
|
|
107
63
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
* @param {string} jsonString - JSON string
|
|
111
|
-
* @returns {{original: number, minified?: number, saved?: number, keys?: number}}
|
|
112
|
-
*/
|
|
113
|
-
function getSizeInfo(jsonString) {
|
|
114
|
-
const info = { original: jsonString.length };
|
|
115
|
-
|
|
116
|
-
try {
|
|
117
|
-
const parsed = JSON.parse(jsonString);
|
|
118
|
-
info.keys = countKeys(parsed);
|
|
119
|
-
|
|
120
|
-
const minified = JSON.stringify(parsed);
|
|
121
|
-
info.minified = minified.length;
|
|
122
|
-
info.saved = jsonString.length - minified.length;
|
|
123
|
-
} catch (e) {
|
|
124
|
-
// Invalid JSON
|
|
125
|
-
}
|
|
64
|
+
// Main
|
|
65
|
+
const args = process.argv.slice(2);
|
|
126
66
|
|
|
127
|
-
|
|
67
|
+
// Check for help
|
|
68
|
+
if (args.includes('--help') || args.includes('-h')) {
|
|
69
|
+
showHelp();
|
|
70
|
+
process.exit(0);
|
|
128
71
|
}
|
|
129
72
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
* @param {any} obj - Object to sort
|
|
133
|
-
* @returns {any} Object with sorted keys
|
|
134
|
-
*/
|
|
135
|
-
function sortKeys(obj) {
|
|
136
|
-
if (obj === null || typeof obj !== 'object') return obj;
|
|
137
|
-
if (Array.isArray(obj)) return obj.map(sortKeys);
|
|
138
|
-
|
|
139
|
-
const sorted = {};
|
|
140
|
-
Object.keys(obj).sort().forEach(key => {
|
|
141
|
-
sorted[key] = sortKeys(obj[key]);
|
|
142
|
-
});
|
|
143
|
-
return sorted;
|
|
144
|
-
}
|
|
73
|
+
const minify = args.includes('--minify') || args.includes('-m');
|
|
74
|
+
const input = args.filter(a => !a.startsWith('-')).join(' ');
|
|
145
75
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
* @param {string} jsonString - JSON string
|
|
149
|
-
* @param {number} [indent=2] - Indentation spaces
|
|
150
|
-
* @returns {{success: boolean, result?: string, error?: string}}
|
|
151
|
-
*/
|
|
152
|
-
function formatSorted(jsonString, indent = 2) {
|
|
76
|
+
if (!input) {
|
|
77
|
+
// Read from stdin
|
|
153
78
|
try {
|
|
154
|
-
const
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
} catch (
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
error: e.message
|
|
164
|
-
};
|
|
79
|
+
const stdin = fs.readFileSync(0, 'utf-8').trim();
|
|
80
|
+
if (!stdin) {
|
|
81
|
+
showHelp();
|
|
82
|
+
process.exit(0);
|
|
83
|
+
}
|
|
84
|
+
formatJSON(stdin, minify);
|
|
85
|
+
} catch (err) {
|
|
86
|
+
console.error('Error reading stdin: ' + err.message);
|
|
87
|
+
process.exit(1);
|
|
165
88
|
}
|
|
89
|
+
} else {
|
|
90
|
+
formatJSON(input, minify);
|
|
166
91
|
}
|
|
167
92
|
|
|
168
|
-
|
|
169
|
-
format,
|
|
170
|
-
minify,
|
|
171
|
-
isValid,
|
|
172
|
-
pretty,
|
|
173
|
-
countKeys,
|
|
174
|
-
getSizeInfo,
|
|
175
|
-
sortKeys,
|
|
176
|
-
formatSorted
|
|
177
|
-
};
|
|
93
|
+
console.log(PRO_UPGRADE);
|
package/package.json
CHANGED
|
@@ -1,27 +1,40 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "devkits-json-formatter",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "Format
|
|
3
|
+
"version": "1.0.4",
|
|
4
|
+
"description": "Format and validate JSON from CLI or stdin — pretty print, minify, and validate JSON files",
|
|
5
5
|
"main": "index.js",
|
|
6
|
-
"
|
|
7
|
-
"index.js",
|
|
8
|
-
"
|
|
9
|
-
|
|
6
|
+
"bin": {
|
|
7
|
+
"jsonfmt": "./index.js",
|
|
8
|
+
"json-formatter": "./index.js"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"test": "node test.js"
|
|
12
|
+
},
|
|
10
13
|
"keywords": [
|
|
11
14
|
"json",
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
"
|
|
17
|
-
"developer-tools"
|
|
18
|
-
"devkits"
|
|
15
|
+
"formatter",
|
|
16
|
+
"validator",
|
|
17
|
+
"cli",
|
|
18
|
+
"pretty-print",
|
|
19
|
+
"minify",
|
|
20
|
+
"developer-tools"
|
|
19
21
|
],
|
|
20
|
-
"author": "DevKits
|
|
22
|
+
"author": "DevKits Team <devkits-auto@protonmail.com>",
|
|
21
23
|
"license": "MIT",
|
|
22
|
-
"repository":
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "https://github.com/devkits/tools"
|
|
27
|
+
},
|
|
28
|
+
"homepage": "https://devkits-tools.surge.sh",
|
|
23
29
|
"bugs": {
|
|
24
|
-
"url": "https://github.com/devkits/
|
|
30
|
+
"url": "https://github.com/devkits/tools/issues"
|
|
31
|
+
},
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=14.0.0"
|
|
25
34
|
},
|
|
26
|
-
"
|
|
35
|
+
"files": [
|
|
36
|
+
"index.js",
|
|
37
|
+
"README.md"
|
|
38
|
+
],
|
|
39
|
+
"dependencies": {}
|
|
27
40
|
}
|