# json-lens

> Lenses with JSON Pointer

Latest version **0.0.1** (published 2014-12-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install json-lens
pnpm add json-lens
yarn add json-lens
bun add json-lens
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.1 |
| Published | 2014-12-08 |
| First published | 2014-12-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Kevin Beaty |
| Maintainers | kevinbeaty |
| Keywords | json, lens |

## Links

- npm: https://www.npmjs.com/package/json-lens
- Repository: https://github.com/kevinbeaty/json-lens
- Homepage: http://github.com/kevinbeaty/json-lens
- Issues: https://github.com/kevinbeaty/json-lens/issues
- npm.io page: https://npm.io/package/json-lens

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 0.0.1 (latest) — 2014-12-08

## README

## JSON Lens
[![Build Status](https://secure.travis-ci.org/kevinbeaty/json-lens.svg)](http://travis-ci.org/kevinbeaty/json-lens)

Create Lenses from [JSON Pointer][0].

Starting with this JSON:

```javascript
var market = {
  foods: {
    fruits: ['apples', 'oranges'],
    veggies: ['peas', 'carrots']
  }
};
```

We can create lenses using JSON pointers.

```javascript
var firstFruit = lens('/foods/fruits/0');
var secondVeggie = lens('/foods/veggies/1');
```

And use them to get values from our JSON object

```javascript
t.equal(firstFruit(market), 'apples');
t.equal(secondVeggie(market), 'carrots');
```

Or set values within it

```javascript
firstFruit.set('bananas', market);
t.equal(firstFruit(market), 'bananas');

secondVeggie.set('onions', market);
t.equal(secondVeggie(market), 'onions');
```

Or set by modifying existing values
```javascript
function ohYeah(v){
  return v+'!';
}

firstFruit.map(ohYeah, market);
t.equal(firstFruit(market), 'bananas!');

secondVeggie.map(ohYeah, market);
t.equal(secondVeggie(market), 'onions!');
```

Retaining the structure of our JSON all along

```javascript
var result = {
  foods: {
    fruits: ['bananas!', 'oranges'],
    veggies: ['peas', 'onions!']
  }
};
t.deepEqual(market, result);
```

### Credits
Pointer get/set code modified from [JSON Pointer][1] and lens implementation from [Ramda][2].  Both projects are licensed under MIT.

[0]: http://tools.ietf.org/html/rfc6901
[1]: https://github.com/manuelstofer/json-pointer
[2]: http://ramdajs.com

---
_Source: https://npm.io/package/json-lens · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
