@peter_marklund/json 0.0.1
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/LICENSE +21 -0
- package/README.md +35 -0
- package/bin/json.js +152 -0
- package/package.json +29 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Peter Marklund
|
|
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.
|
package/README.md
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# @peter_marklund/json
|
|
2
|
+
|
|
3
|
+
An npm package that provides a convenient way to work with JSON using JavaScript in the terminal.
|
|
4
|
+
|
|
5
|
+
## TODO
|
|
6
|
+
|
|
7
|
+
* Use stable/sorted JSON stringify
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
npm install @peter_marklund/json -g
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
echo '{"foo": "1"}' | json .foo
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Developing the Library Locally
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
npm link
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Publishing a new Version
|
|
28
|
+
|
|
29
|
+
```sh
|
|
30
|
+
npm publish --access public
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
# Prior Art
|
|
34
|
+
|
|
35
|
+
* [trentm/json](https://github.com/trentm/json) - nice library with [very good documentation](https://trentm.com/json/)
|
package/bin/json.js
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
#!/usr/bin/env node --max-old-space-size=16384
|
|
2
|
+
|
|
3
|
+
// Simple node script that provides a jq alternative for processing JSON using JavaScript on the command line.
|
|
4
|
+
// Supports newline separated JSON (JSON lines) as well as JSON data.
|
|
5
|
+
// Also supports log data where some lines are JSON and some aren't and log lines where the
|
|
6
|
+
// beginning of the line is text and the end of the line is JSON, i.e.:
|
|
7
|
+
//
|
|
8
|
+
// 2022-10-18T14:07:53.960Z [INFO ] starting server with config: {"port":3000}
|
|
9
|
+
|
|
10
|
+
// With pipe (can break for large amounts of JSON data)
|
|
11
|
+
//
|
|
12
|
+
// cat some-data.json | json <javascript-code>
|
|
13
|
+
//
|
|
14
|
+
// With a file path (works better for large amounts of data):
|
|
15
|
+
//
|
|
16
|
+
// json <javascript-code> <file-path>
|
|
17
|
+
// The JSON data is available in a `data` variable. If the JavaScript code argument starts with a "." then that is equivalent to starting it with "data.".
|
|
18
|
+
//
|
|
19
|
+
// You can use map and filter and useful lodash functions like get:
|
|
20
|
+
//
|
|
21
|
+
// cat some-data.json | json ".Items.map(i => ({id: get(i, 'id.S'), updatedAt: get(i, 'updatedAt.N')})).filter(i => new Date(Number(i.updatedAt)) < new Date())"
|
|
22
|
+
//
|
|
23
|
+
// You can access the JSON data with the data variable:
|
|
24
|
+
//
|
|
25
|
+
// cat some-data.json | json "Object.keys(data)"
|
|
26
|
+
//
|
|
27
|
+
// Split complex processing with pipes if it helps readability:
|
|
28
|
+
//
|
|
29
|
+
// cat some-data.json \
|
|
30
|
+
// | json ".Items.map(i => ({id: get(i, 'id.S'), updatedAt: get(i, 'updatedAt.N')}))" \
|
|
31
|
+
// | json ".filter(i => new Date(Number(i.updatedAt)) < new Date())"
|
|
32
|
+
//
|
|
33
|
+
// Easily print lengths of arrays or keys of objects etc:
|
|
34
|
+
//
|
|
35
|
+
// cat some-data.json | json ".Items.length"
|
|
36
|
+
//
|
|
37
|
+
// Pretty print (echo) JSON data:
|
|
38
|
+
//
|
|
39
|
+
// cat some-data.json | json .
|
|
40
|
+
|
|
41
|
+
const fs = require("fs");
|
|
42
|
+
const readline = require("readline");
|
|
43
|
+
const _ = require("lodash");
|
|
44
|
+
Object.assign(global, require("lodash"));
|
|
45
|
+
const { diff } = require("object-diffy");
|
|
46
|
+
const { colorize } = require("json-colorizer");
|
|
47
|
+
|
|
48
|
+
function getCodeArg() {
|
|
49
|
+
let code = process.argv[2] || "data"
|
|
50
|
+
// Support jq like dot syntax
|
|
51
|
+
if (code === ".") {
|
|
52
|
+
code = "data"
|
|
53
|
+
} else if (code.startsWith(".")) {
|
|
54
|
+
code = "data" + code
|
|
55
|
+
}
|
|
56
|
+
return code
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function readStdIn() {
|
|
60
|
+
return fs.readFileSync(0).toString();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async function jsonIn(filePath) {
|
|
64
|
+
let textInput
|
|
65
|
+
try {
|
|
66
|
+
if (filePath) {
|
|
67
|
+
return require(filePath)
|
|
68
|
+
} else {
|
|
69
|
+
textInput = readStdIn()
|
|
70
|
+
// NOTE: I've found JSON.parse intermittently errors out for data sizes around 15 MB but require(filePath) can handle more?
|
|
71
|
+
// const dataSizeMb = Buffer.byteLength(textInput) / 1024 / 1024
|
|
72
|
+
return JSON.parse(textInput)
|
|
73
|
+
}
|
|
74
|
+
} catch (jsonErr) {
|
|
75
|
+
try {
|
|
76
|
+
const lines = []
|
|
77
|
+
let openLines = []
|
|
78
|
+
let nErrors = 0
|
|
79
|
+
let lineCount = 0
|
|
80
|
+
const processLine = (line) => {
|
|
81
|
+
lineCount += 1
|
|
82
|
+
const result = parseLine(line, openLines)
|
|
83
|
+
if (result.error) {
|
|
84
|
+
console.log(error)
|
|
85
|
+
nErrors += 1
|
|
86
|
+
}
|
|
87
|
+
if (result.parsedLine) lines.push(result.parsedLine)
|
|
88
|
+
openLines = result.openLines
|
|
89
|
+
}
|
|
90
|
+
if (filePath) {
|
|
91
|
+
const rl = readline.createInterface({
|
|
92
|
+
input: fs.createReadStream(filePath),
|
|
93
|
+
crlfDelay: Infinity,
|
|
94
|
+
})
|
|
95
|
+
for await (const line of rl) {
|
|
96
|
+
processLine(line)
|
|
97
|
+
}
|
|
98
|
+
} else {
|
|
99
|
+
textInput = textInput || readStdIn()
|
|
100
|
+
const textLines = textInput
|
|
101
|
+
.trim()
|
|
102
|
+
.split("\n")
|
|
103
|
+
.map((l) => l.trim())
|
|
104
|
+
.filter(Boolean)
|
|
105
|
+
for (const [index, line] of textLines.entries()) {
|
|
106
|
+
processLine(line)
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
if (nErrors > 0)
|
|
110
|
+
console.log(
|
|
111
|
+
`Failed to parse ${nErrors}/${textLines.length} lines due to errors`
|
|
112
|
+
)
|
|
113
|
+
return lines
|
|
114
|
+
} catch (linesErr) {
|
|
115
|
+
console.log(jsonErr.stack)
|
|
116
|
+
console.log(linesErr.stack)
|
|
117
|
+
throw new Error("Could not parse input as JSON or as JSON lines")
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function printJson(data) {
|
|
123
|
+
console.log(JSON.stringify(data))
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function printPrettyJson(data) {
|
|
127
|
+
console.log(colorize(JSON.stringify(data, null, 4)))
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
async function main() {
|
|
131
|
+
const code = getCodeArg()
|
|
132
|
+
const filePath = process.argv[3]
|
|
133
|
+
|
|
134
|
+
const data = await jsonIn(filePath)
|
|
135
|
+
|
|
136
|
+
const processedData = eval(code)
|
|
137
|
+
|
|
138
|
+
if (process.env.RAW === "true" || (process.env.RAW !== "false" && ["string", "number", "boolean"].includes(typeof processedData))) {
|
|
139
|
+
console.log(processedData)
|
|
140
|
+
} else if (process.env.JSONL === "true" && Array.isArray(processedData)) {
|
|
141
|
+
printJsonLines(processedData)
|
|
142
|
+
} else if (process.env.PRETTY === "false") {
|
|
143
|
+
printJson(processedData)
|
|
144
|
+
} else {
|
|
145
|
+
printPrettyJson(processedData)
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
if (require.main === module) {
|
|
151
|
+
main()
|
|
152
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@peter_marklund/json",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "A convenient way to work with JSON using JavaScript in the terminal",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/peter/json.git"
|
|
12
|
+
},
|
|
13
|
+
"bin": {
|
|
14
|
+
"json": "./bin/json.js"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [],
|
|
17
|
+
"author": "",
|
|
18
|
+
"license": "ISC",
|
|
19
|
+
"type": "commonjs",
|
|
20
|
+
"bugs": {
|
|
21
|
+
"url": "https://github.com/peter/json/issues"
|
|
22
|
+
},
|
|
23
|
+
"homepage": "https://github.com/peter/json#readme",
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"json-colorizer": "^3.0.1",
|
|
26
|
+
"lodash": "^4.17.23",
|
|
27
|
+
"object-diffy": "^1.0.4"
|
|
28
|
+
}
|
|
29
|
+
}
|