json-p3 0.1.0 → 0.2.0
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 +26 -211
- package/dist/index.d.ts +2 -2
- package/dist/json-p3.cjs.js +279 -45
- package/dist/json-p3.esm.js +277 -46
- package/dist/json-p3.iife.js +279 -45
- package/dist/json-p3.iife.min.js +1 -1
- package/dist/json-p3.iife.min.js.map +1 -1
- package/dist/patch/patch.d.ts +4 -0
- package/dist/path/environment.d.ts +56 -10
- package/dist/path/errors.d.ts +8 -0
- package/dist/path/expression.d.ts +1 -0
- package/dist/path/index.d.ts +12 -2
- package/dist/path/path.d.ts +10 -1
- package/dist/path/selectors.d.ts +0 -1
- package/dist/pointer/errors.d.ts +15 -0
- package/dist/pointer/index.d.ts +1 -1
- package/dist/pointer/pointer.d.ts +31 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types.d.ts +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -6,76 +6,19 @@ JSONPath, JSON Patch and JSON Pointer for JavaScript.
|
|
|
6
6
|
|
|
7
7
|
<p align="center">
|
|
8
8
|
<a href="https://github.com/jg-rp/json-p3/blob/main/LICENSE">
|
|
9
|
-
<img alt="
|
|
9
|
+
<img alt="LICENSE" src="https://img.shields.io/npm/l/json-p3?style=flat-square">
|
|
10
10
|
</a>
|
|
11
11
|
<a href="https://github.com/jg-rp/json-p3/actions">
|
|
12
12
|
<img src="https://img.shields.io/github/actions/workflow/status/jg-rp/json-p3/tests.yaml?branch=main&label=tests&style=flat-square" alt="Tests">
|
|
13
13
|
</a>
|
|
14
|
-
<
|
|
14
|
+
<a href="https://www.npmjs.com/package/json-p3">
|
|
15
|
+
<img alt="NPM" src="https://img.shields.io/npm/v/json-p3?style=flat-square">
|
|
16
|
+
</a>
|
|
17
|
+
<img alt="npm type definitions" src="https://img.shields.io/npm/types/json-p3?style=flat-square">
|
|
15
18
|
</p>
|
|
16
19
|
|
|
17
20
|
---
|
|
18
21
|
|
|
19
|
-
## Install
|
|
20
|
-
|
|
21
|
-
### Node.js
|
|
22
|
-
|
|
23
|
-
Use npm:
|
|
24
|
-
|
|
25
|
-
```console
|
|
26
|
-
npm install --save json-p3
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
Or yarn:
|
|
30
|
-
|
|
31
|
-
```console
|
|
32
|
-
yarn add json-p3
|
|
33
|
-
```
|
|
34
|
-
|
|
35
|
-
Or pnpm:
|
|
36
|
-
|
|
37
|
-
```console
|
|
38
|
-
pnpm add json-p3
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
And use ES module imports:
|
|
42
|
-
|
|
43
|
-
```javascript
|
|
44
|
-
import { query } from "json-p3";
|
|
45
|
-
|
|
46
|
-
const data = {
|
|
47
|
-
players: [{ name: "Sue" }, { name: "John" }, { name: "Sally" }],
|
|
48
|
-
visitors: [{ name: "Brian" }, { name: "Roy" }],
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
const nodes = query("$..name", data);
|
|
52
|
-
console.log(nodes.values());
|
|
53
|
-
// [ 'Sue', 'John', 'Sally', 'Brian', 'Roy' ]
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
Or CommonJS modules:
|
|
57
|
-
|
|
58
|
-
```javascript
|
|
59
|
-
const { query } = require("json-p3");
|
|
60
|
-
|
|
61
|
-
const data = {
|
|
62
|
-
players: [{ name: "Sue" }, { name: "John" }, { name: "Sally" }],
|
|
63
|
-
visitors: [{ name: "Brian" }, { name: "Roy" }],
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
const nodes = query("$..name", data);
|
|
67
|
-
console.log(nodes.values());
|
|
68
|
-
// [ 'Sue', 'John', 'Sally', 'Brian', 'Roy' ]
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
### Browser
|
|
72
|
-
|
|
73
|
-
TODO:
|
|
74
|
-
|
|
75
|
-
## JSONPath
|
|
76
|
-
|
|
77
|
-
Retrieve values from JSON-like data using JSONPath query expressions. We strictly follow standards described in the [IETF JSONPath draft](https://datatracker.ietf.org/doc/html/draft-ietf-jsonpath-base-20).
|
|
78
|
-
|
|
79
22
|
```javascript
|
|
80
23
|
import { jsonpath } from "json-p3";
|
|
81
24
|
|
|
@@ -92,161 +35,33 @@ const nodes = jsonpath.query("$.users[?@.score < 100].name", data);
|
|
|
92
35
|
console.log(nodes.values()); // [ 'John', 'Sally', 'Jane' ]
|
|
93
36
|
```
|
|
94
37
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
- `value` - The value found in the target JSON value. This could be an array, object or primitive value.
|
|
98
|
-
- `location` - An array of property names and array indices that were required to reach the node's value in the target JSON value.
|
|
99
|
-
- `path` - The normalized JSONPath to this node in the target JSON document.
|
|
100
|
-
|
|
101
|
-
Use `JSONPathNodeList.paths()` to retrieve all node paths.
|
|
102
|
-
|
|
103
|
-
```javascript
|
|
104
|
-
// .. continued from above
|
|
105
|
-
console.log(nodes.paths());
|
|
106
|
-
```
|
|
107
|
-
|
|
108
|
-
**Output:**
|
|
38
|
+
## Links
|
|
109
39
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
40
|
+
- Docs: https://jg-rp.github.io/json-p3/
|
|
41
|
+
- Install: https://jg-rp.github.io/json-p3/#install
|
|
42
|
+
- JSONPath syntax: https://jg-rp.github.io/json-p3/guides/jsonpath-syntax
|
|
43
|
+
- API reference: https://jg-rp.github.io/json-p3/api
|
|
44
|
+
- Change log: https://github.com/jg-rp/json-p3/blob/main/CHANGELOG.md
|
|
45
|
+
- NPM: https://www.npmjs.com/package/json-p3
|
|
46
|
+
- Issue tracker: https://github.com/jg-rp/json-p3/issues
|
|
117
47
|
|
|
118
|
-
|
|
48
|
+
## Bundles
|
|
119
49
|
|
|
120
|
-
|
|
121
|
-
// .. continued from above
|
|
122
|
-
console.log(nodes.locations());
|
|
123
|
-
```
|
|
50
|
+
JSON P3 is written in TypeScript, compiled to JavaScript using [Babel](https://babeljs.io/), and bundled using [Rollup](https://rollupjs.org/introduction/). The following, included bundles target `defaults, maintained node version`, as defined by [Browserslist](https://browsersl.ist/#q=defaults%2C+maintained+node+versions).
|
|
124
51
|
|
|
125
|
-
|
|
52
|
+
JSON P3 has zero runtime dependencies.
|
|
126
53
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
```
|
|
54
|
+
| Bundle | Description |
|
|
55
|
+
| --------------------- | -------------------------------------------------------------------------- |
|
|
56
|
+
| `json-p3.cjs.js` | A CommonJS formatted bundle. |
|
|
57
|
+
| `json-p3.esm.js` | An ECMAScript module formatted bundle. |
|
|
58
|
+
| `json-p3-iife.js` | A bundle formatted as an Immediately Invoked Function Expression. |
|
|
59
|
+
| `json-p3-iife.min.js` | A minified bundle formatted as an Immediately Invoked Function Expression. |
|
|
134
60
|
|
|
135
|
-
|
|
61
|
+
## Contributing
|
|
136
62
|
|
|
137
|
-
|
|
63
|
+
Please see [Contributing to JSON P3](https://github.com/jg-rp/json-p3/blob/main/CONTRIBUTING.md)
|
|
138
64
|
|
|
139
|
-
|
|
140
|
-
import { jsonpath } from "json-p3";
|
|
141
|
-
|
|
142
|
-
const data = {
|
|
143
|
-
users: [
|
|
144
|
-
{ name: "Sue", score: 100 },
|
|
145
|
-
{ name: "John", score: 86 },
|
|
146
|
-
{ name: "Sally", score: 84 },
|
|
147
|
-
{ name: "Jane", score: 55 },
|
|
148
|
-
],
|
|
149
|
-
};
|
|
150
|
-
|
|
151
|
-
const path = jsonpath.compile("$.users[?@.score < 100].name");
|
|
152
|
-
const nodes = path.query(data);
|
|
153
|
-
console.log(nodes.values()); // [ 'John', 'Sally', 'Jane' ]
|
|
154
|
-
```
|
|
155
|
-
|
|
156
|
-
## JSON Pointer
|
|
157
|
-
|
|
158
|
-
Identify a single value in JSON-like data, as per RFC 6901. Use `jsonpointer.resolve()` to retrieve the value.
|
|
159
|
-
|
|
160
|
-
```javascript
|
|
161
|
-
import { jsonpointer } from "json-p3";
|
|
162
|
-
|
|
163
|
-
const data = {
|
|
164
|
-
users: [
|
|
165
|
-
{ name: "Sue", score: 100 },
|
|
166
|
-
{ name: "John", score: 86 },
|
|
167
|
-
{ name: "Sally", score: 84 },
|
|
168
|
-
{ name: "Jane", score: 55 },
|
|
169
|
-
],
|
|
170
|
-
};
|
|
171
|
-
|
|
172
|
-
const rv = jsonpointer.resolve("/users/1", data);
|
|
173
|
-
console.log(rv); // { name: 'John', score: 86 }
|
|
174
|
-
```
|
|
175
|
-
|
|
176
|
-
If the pointer can't be resolved against the argument JSON value, one of `JSONPointerIndexError`, `JSONPointerKeyError` or `JSONPointerTypeError` is thrown. All three exceptions inherit from `JSONPointerResolutionError`.
|
|
177
|
-
|
|
178
|
-
```javascript
|
|
179
|
-
import { jsonpointer } from "json-p3";
|
|
180
|
-
|
|
181
|
-
const data = {
|
|
182
|
-
users: [
|
|
183
|
-
{ name: "Sue", score: 100 },
|
|
184
|
-
{ name: "John", score: 86 },
|
|
185
|
-
{ name: "Sally", score: 84 },
|
|
186
|
-
{ name: "Jane", score: 55 },
|
|
187
|
-
],
|
|
188
|
-
};
|
|
189
|
-
|
|
190
|
-
const rv = jsonpointer.resolve("/users/1/age", data);
|
|
191
|
-
// JSONPointerKeyError: no such property ("/users/1/age")
|
|
192
|
-
```
|
|
193
|
-
|
|
194
|
-
A fallback value can be given as a third argument, which will be returned in the event of a `JSONPointerResolutionError`.
|
|
195
|
-
|
|
196
|
-
```javascript
|
|
197
|
-
import { jsonpointer } from "json-p3";
|
|
198
|
-
|
|
199
|
-
const data = {
|
|
200
|
-
users: [
|
|
201
|
-
{ name: "Sue", score: 100 },
|
|
202
|
-
{ name: "John", score: 86 },
|
|
203
|
-
{ name: "Sally", score: 84 },
|
|
204
|
-
{ name: "Jane", score: 55 },
|
|
205
|
-
],
|
|
206
|
-
};
|
|
207
|
-
|
|
208
|
-
const rv = jsonpointer.resolve("/users/1/age", data, -1);
|
|
209
|
-
console.log(rv); // -1
|
|
210
|
-
```
|
|
211
|
-
|
|
212
|
-
TODO: "compile" a pointer for later use
|
|
213
|
-
|
|
214
|
-
## JSON Patch
|
|
215
|
-
|
|
216
|
-
Apply a JSON Patch ([RFC 6902](https://datatracker.ietf.org/doc/html/rfc6902)) to some data. A JSON Patch defines update operation to perform on a JSON document. **Data is modified in place.**.
|
|
217
|
-
|
|
218
|
-
```javascript
|
|
219
|
-
import { jsonpatch } from "json-p3";
|
|
220
|
-
|
|
221
|
-
const ops = [
|
|
222
|
-
{ op: "add", path: "/some/foo", value: { foo: {} } },
|
|
223
|
-
{ op: "add", path: "/some/foo", value: { bar: [] } },
|
|
224
|
-
{ op: "copy", from: "/some/other", path: "/some/foo/else" },
|
|
225
|
-
{ op: "add", path: "/some/foo/bar/-", value: 1 },
|
|
226
|
-
];
|
|
227
|
-
|
|
228
|
-
const data = { some: { other: "thing" } };
|
|
229
|
-
jsonpatch.apply(ops, data);
|
|
230
|
-
console.log(data);
|
|
231
|
-
// { some: { other: 'thing', foo: { bar: [Array], else: 'thing' } } }
|
|
232
|
-
```
|
|
233
|
-
|
|
234
|
-
Use the `JSONPatch` class to create a patch for repeated application.
|
|
235
|
-
|
|
236
|
-
```javascript
|
|
237
|
-
import { JSONPatch } from "json-p3";
|
|
238
|
-
|
|
239
|
-
const patch = new JSONPatch([
|
|
240
|
-
{ op: "add", path: "/some/foo", value: { foo: {} } },
|
|
241
|
-
{ op: "add", path: "/some/foo", value: { bar: [] } },
|
|
242
|
-
{ op: "copy", from: "/some/other", path: "/some/foo/else" },
|
|
243
|
-
{ op: "add", path: "/some/foo/bar/-", value: 1 },
|
|
244
|
-
]);
|
|
245
|
-
|
|
246
|
-
const data = { some: { other: "thing" } };
|
|
247
|
-
patch.apply(data);
|
|
248
|
-
console.log(data);
|
|
249
|
-
// { some: { other: 'thing', foo: { bar: [Array], else: 'thing' } } }
|
|
250
|
-
```
|
|
65
|
+
## License
|
|
251
66
|
|
|
252
|
-
|
|
67
|
+
`json-p3` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
export declare const version = "__VERSION__";
|
|
2
2
|
export * as jsonpath from "./path";
|
|
3
|
-
export { FunctionExpressionType, JSONPath, JSONPathEnvironment, JSONPathError, JSONPathIndexError, JSONPathLexerError, JSONPathNode, JSONPathNodeList, JSONPathSyntaxError, JSONPathTypeError, Token, TokenKind, Nothing, query, compile, } from "./path";
|
|
3
|
+
export { DEFAULT_ENVIRONMENT, FunctionExpressionType, JSONPath, JSONPathEnvironment, JSONPathError, JSONPathIndexError, JSONPathLexerError, JSONPathNode, JSONPathNodeList, JSONPathSyntaxError, JSONPathTypeError, JSONPathRecursionLimitError, Token, TokenKind, Nothing, query, compile, } from "./path";
|
|
4
4
|
export type { JSONPathEnvironmentOptions, FilterFunction } from "./path";
|
|
5
5
|
export * as jsonpointer from "./pointer";
|
|
6
|
-
export { JSONPointer, resolve, UNDEFINED } from "./pointer";
|
|
6
|
+
export { JSONPointer, RelativeJSONPointer, resolve, UNDEFINED, } from "./pointer";
|
|
7
7
|
export * as jsonpatch from "./patch";
|
|
8
8
|
export { JSONPatch, JSONPatchError, JSONPatchTestFailure, apply, } from "./patch";
|
|
9
9
|
export type { OpObject } from "./patch";
|