https://github.com/terraindata/yadeep
Yet another deep JSON getter/setter module
https://github.com/terraindata/yadeep
deep get js json set
Last synced: 4 months ago
JSON representation
Yet another deep JSON getter/setter module
- Host: GitHub
- URL: https://github.com/terraindata/yadeep
- Owner: terraindata
- License: other
- Created: 2018-12-26T23:07:03.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-07T04:14:58.000Z (over 2 years ago)
- Last Synced: 2024-04-26T18:43:31.333Z (about 1 year ago)
- Topics: deep, get, js, json, set
- Language: TypeScript
- Size: 884 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# yadeep
[](https://travis-ci.org/terraindata/yadeep)
[](https://www.npmjs.org/package/yadeep)
[](https://david-dm.org/terraindata/yadeep)
[](https://david-dm.org/terraindata/yadeep#info=devDependencies)_Yet another deep JSON getter/setter module_
## Why?
Several libraries already exist for getting and setting fields in deep JSON objects. However,
none had the flexibility we needed for building an enterprise-quality ETL solution.A key differentiator is that `yadeep` builds on top of [terrain-keypath](https://github.com/terraindata/terrain-keypath).
This means:* We can work with **any** field name in a JSON document. Do you have field names like `'*'`, `'.'`, etc.? yadeep has
you covered. Most libraries sacrifice this generality to use characters like those as waypoint delimiters,
wildcards, etc.
* We can get and set on **wildcard** keypaths. Do you want to increment the values of all children of an object? Or
are you writing some aggregation function on deeply-nested sub-documents? yadeep will help.
* We can also handle some level of **nested wildcards**. Keypath expressions with multiple wildcard tokens will try
to behave in a semantically intuitive way (though expressions with more than two wildcard tokens are quite exotic
in the real world and hence not well-tested).yadeep also aims to provide the simplest syntax it can while still supporting this level of generality. If you accept
our keypath conventions, you can immediately use `yadeep.get(object, keypath)` and `yadeep.set(object, keypath, value)`;
the aim is to make the API human readable and somewhat self-documenting.Additional functions for searching in and traversing deep JSON objects can be found in the [code](https://github.com/terraindata/yadeep/blob/master/yadeep.ts).
TypeScript definitions included!
## Installation
npm install yadeep
## Basic Usage
```js
const { KeyPath } = require('terrain-keypath');
const yadeep = require('yadeep');const doc = {
name: 'Bob',
hardarr: [['a'], ['b', [{count: 1}, {count: 2}]]]
};yadeep.get(doc, KeyPath(['hardarr', '1', '0'])); // 'b'
yadeep.get(doc, KeyPath(['hardarr', '1', '1', -1, 'count'])); // [1, 2]yadeep.set(doc, KeyPath(['hardarr', '0', '0']), 'not a'); // changes 'a' -> 'not a'
yadeep.set(doc, KeyPath(['hardarr', '1', '1', -1, 'count']), 17); // changes both 'counts's to 17
```See [yadeepTests.ts](https://github.com/terraindata/yadeep/blob/master/yadeepTests.ts) for a battery of additional tests and usages.