Add json.js

2025-09-20 22:56:48 +00:00
commit 72eb377d18

31
json.js.md Normal file

@@ -0,0 +1,31 @@
```javascript
import { jsonQuery, jsonPatch, tokenizePath } from './json.js';
```
## jsonQuery(model, path, ?default) → mixed
Lookup a key, or an array of keys from the structure in `model`. The path uses a simplified syntax to select the nodes:
- `.foo` - the node **foo** in the root.
- `.items[].description` - an array of the the **description** values from each of the items in the items array.
- `.users[0].username` - the username of the first item in the array users.
```json
let model = { foo: true, bar: 42, baz: "hello" };
let bar = jsonQuery(model, '.bar', 43);
// bar = 42
```
## jsonPatch(model, path, value) → object
Patch a key in the model with the provided value.
```json
let model = { foo: true, bar: 42, baz: "hello" };
let updated = jsonPatch(model, '.baz', 'hello world');
// updated = { foo: true, bar: 42, baz: "hello world" }
```
## tokenizePath(path) → array
This is used internally to parse paths, but it may be useful for some other purpose. There is no backward compatibility promise for this function however.