Clone
1
json.js
Chris edited this page 2025-09-20 22:56:48 +00:00
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.
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.

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.