@peter_marklund/json 0.0.5 → 0.0.6

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 CHANGED
@@ -1,10 +1,6 @@
1
1
  # @peter_marklund/json
2
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
3
+ An npm package that provides a convenient way to use JavaScript to work with JSON in the terminal. This tool is for those of us who like `jq` but prefer JavaScript over `jq` syntax.
8
4
 
9
5
  ## Installation
10
6
 
@@ -14,9 +10,84 @@ npm install @peter_marklund/json -g
14
10
 
15
11
  ## Usage
16
12
 
13
+ The JSON data is typically passed to the `json` command via stdin but can also be passed as a file path via the second argument. The first argument to the `json` command is a string with JavaScript code to be evaluated. All [lodash](https://lodash.com/docs/4.17.23) functions (i.e. `pick`, `pickBy`, `mapValues`, `sum` etc.) are available as are a number of [helper functions](src/helpers.js). It is also possible to provide custom JavaScript helper functions via the `JSON_HELPERS_PATH` environment variable.
14
+
17
15
  ```sh
16
+ # Get the value at a path
18
17
  echo '{"foo": "1"}' | json .foo
19
18
 
19
+ # Get the keys of the JSON data
20
+ cat test/input/basic.json | json 'Object.keys(data)'
21
+ # [
22
+ # "foo",
23
+ # "bar",
24
+ # "baz",
25
+ # "nested",
26
+ # "data"
27
+ # ]
28
+
29
+ # Get the length of an array:
30
+ cat test/input/basic.json | json '.data.length'
31
+ # 3
32
+
33
+ # Use lodash functions
34
+ cat test/input/basic.json | json '.data.map(d => pick(d, ["value"]))'
35
+ # [
36
+ # {
37
+ # "value": 100
38
+ # },
39
+ # {
40
+ # "value": 200
41
+ # },
42
+ # {
43
+ # "value": 300
44
+ # }
45
+ # ]
46
+
47
+ # Use the flattenJson helper to find the path of a deeply nested value:
48
+ cat test/input/basic.json | json 'flattenJson(data)'
49
+ # {
50
+ # "bar": "Hello world",
51
+ # "baz": false,
52
+ # "data.0.id": 1,
53
+ # "data.0.name": "Item 1",
54
+ # "data.0.value": 100,
55
+ # "data.1.id": 2,
56
+ # "data.1.name": "Item 2",
57
+ # "data.1.value": 200,
58
+ # "data.2.id": 3,
59
+ # "data.2.name": "Item 3",
60
+ # "data.2.value": 300,
61
+ # "foo": 1,
62
+ # "nested.foo.bar": "nested value"
63
+ # }
64
+
65
+ # Use the stats helper function to get min/max/avg/median/p90 etc. for numerical values
66
+ cat test/input/basic.json | json 'data.data.map(d => d.value)' | json 'stats(data)'
67
+ # {
68
+ # "avg": 200,
69
+ # "count": 3,
70
+ # "max": 300,
71
+ # "min": 100,
72
+ # "p1": 102,
73
+ # "p10": 120,
74
+ # "p20": 140,
75
+ # "p30": 160,
76
+ # "p40": 180,
77
+ # "p5": 110,
78
+ # "p50": 200,
79
+ # "p60": 220,
80
+ # "p70": 240,
81
+ # "p80": 260,
82
+ # "p90": 280,
83
+ # "p95": 290,
84
+ # "p99": 298,
85
+ # "p999": 299.79999999999995,
86
+ # "stdDev": 81.64965809277261,
87
+ # "sum": 600
88
+ # }
89
+
90
+
20
91
  # Colorized pretty printing is the default
21
92
  cat test/input/array.json | json
22
93
  # [
@@ -67,6 +138,10 @@ cat test/input/log-with-json.log | json
67
138
  # },
68
139
  # ...
69
140
  # ]
141
+
142
+ # Using custom helper functions via the JSON_HELPERS_PATH env var and a javascript module with exported functions
143
+ echo '{"values1": [1, 2, 3, 4], "values2": [3, 5, 1, 11]}' | JSON_HELPERS_PATH="$(pwd)/test/custom-helpers.js" json 'correlation(data.values1, data.values2)'
144
+ # 0.5976143046671968
70
145
  ```
71
146
 
72
147
  ## Running the Tests
@@ -86,4 +161,5 @@ npm publish --access public
86
161
 
87
162
  # Prior Art
88
163
 
164
+ * [jq](https://github.com/jqlang/jq) - the standard for processing JSON in the terminal
89
165
  * [trentm/json](https://github.com/trentm/json) - nice library with [very good documentation](https://trentm.com/json/)
package/bin/json.js CHANGED
@@ -40,9 +40,14 @@
40
40
 
41
41
  const fs = require("fs");
42
42
  const readline = require("readline");
43
+
43
44
  const _ = require("lodash");
44
45
  Object.assign(global, require("lodash"));
45
46
  Object.assign(global, require("../src/helpers.js"));
47
+ if (process.env.JSON_HELPERS_PATH) {
48
+ Object.assign(global, require(process.env.JSON_HELPERS_PATH));
49
+ }
50
+
46
51
  const { diff } = require("object-diffy");
47
52
  const { colorize } = require("json-colorizer");
48
53
  const stringify = require('fast-json-stable-stringify')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peter_marklund/json",
3
- "version": "0.0.5",
3
+ "version": "0.0.6",
4
4
  "description": "A convenient way to work with JSON using JavaScript in the terminal",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -0,0 +1,20 @@
1
+ function correlation(x, y) {
2
+ const n = x.length;
3
+ const meanX = x.reduce((a, b) => a + b, 0) / n;
4
+ const meanY = y.reduce((a, b) => a + b, 0) / n;
5
+
6
+ let num = 0, denomX = 0, denomY = 0;
7
+ for (let i = 0; i < n; i++) {
8
+ const dx = x[i] - meanX;
9
+ const dy = y[i] - meanY;
10
+ num += dx * dy;
11
+ denomX += dx * dx;
12
+ denomY += dy * dy;
13
+ }
14
+
15
+ return num / Math.sqrt(denomX * denomY);
16
+ }
17
+
18
+ module.exports = {
19
+ correlation,
20
+ }
@@ -64,6 +64,11 @@
64
64
  "name": "log_with_json_input",
65
65
  "command": "cat test/input/log-with-json.log | json 'sum(data.map(d => d.duration_ms))'",
66
66
  "expected": "141"
67
+ },
68
+ {
69
+ "name": "custom_helper_functions",
70
+ "command": "echo '{\"values1\": [1, 2, 3, 4], \"values2\": [3, 5, 1, 11]}' | JSON_HELPERS_PATH=\"$(pwd)/test/custom-helpers.js\" json 'round(correlation(data.values1, data.values2), 3)'",
71
+ "expected": "0.598"
67
72
  }
68
73
  ]
69
74
  }