# jsonld-patch

> JSON patch for JSON-LD

Latest version **0.1.0** (published 2018-05-11) · 0 weekly downloads

## Install

```sh
npm install jsonld-patch
pnpm add jsonld-patch
yarn add jsonld-patch
bun add jsonld-patch
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2018-05-11 |
| First published | 2018-05-11 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=8.9.0 |
| Dependencies | 3 |
| Unpacked size | 181 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 7 |
| Author | Digital Bazaar, Inc. |
| Maintainers | dlongley |

## Links

- npm: https://www.npmjs.com/package/jsonld-patch
- Repository: https://github.com/digitalbazaar/jsonld-patch
- Issues: https://github.com/digitalbazaar/jsonld-patch/issues
- npm.io page: https://npm.io/package/jsonld-patch

## Dependencies (3)

- [jsonld](https://npm.io/package/jsonld.md) ^1.0.1
- [lodash](https://npm.io/package/lodash.md) ^4.17.10
- [fast-json-patch](https://npm.io/package/fast-json-patch.md) ^2.0.6

## Recent versions

- 0.1.0 (latest) — 2018-05-11

## README

# JSON-LD Patch

This library provides an API for applying JSON patches to JSON-LD documents.

JSON patches may be represented in JSON-LD by using hte will be interpreted as JSON-LD using the JSON-LD Patch
`@context`.

## The API

  * api.applyPatch(document, patch)

## Quick Examples

### Installation

```
npm install jsonld-patch
```

### Simple patch

```js
const jldp = require('jsonld-patch');

const document = {
  "@context": "http://schema.org/",
  "@type": "Person",
  "name": "Alice"
};

const patch = [
  {op: 'add', path: '/email', value: 'pdoe@example.com'}
];

const {newDocument} = jldp.applyPatch({document, patch});

/*
newDocument is:
{
  "@context": "http://schema.org/",
  "@type": "Person",
  "name": "Alice",
  "email": "alice@example.com"
}
*/
```

### Contextual patch

```js
const document = {
  "@type": "http://schema.org/Person",
  "http://schema.org/name": "Alice"
};

const patch = [
  {op: 'add', path: '/email', value: 'alice@example.com'}
];

const context = {
  "@context": "http://schema.org/"
};

// document will be compacted to the new context, then patched
const {newDocument} = jldp.applyPatch({document, patch, context});

/*
newDocument is:
{
  "@context": "http://schema.org/",
  "@type": "Person",
  "name": "Alice",
  "email": "alice@example.com"
}
*/

```

### Frame and patch

```js
const document = {
  "@context": "http://schema.org/",
  "@id": "http://example.com/alice",
  "@type": "Person",
  "name": "Alice",
  "knows": {
    "@id": "http://example.com/bob",
    "@type": "Person",
    "name": "Bob",
    "knows": "http://example.com/alice"
  }
};

const patch = [
  {op: 'add', path: '/email', value: 'bob@example.com'}
];

const frame = {
  "@context": "http://schema.org/",
  "@id": "http://example.com/bob"
}

const {newDocument} = jldp.applyPatch({document, patch, frame});

/*
newDocument is:
{
  "@context": "http://schema.org/",
  "@type": "Person",
  "@id": "http://example.com/bob",
  "name": "Bob",
  "email": "bob@example.com",
  "knows": {
    "@id": "http://example.com/alice",
    "@type": "Person",
    "name": "Alice",
    "knows": "http://example.com/bob"
  }
}
*/

// Note: newDocument could be reframed back to alice at its root using jsonld:

const aliceFrame = {
  "@context": "http://schema.org/",
  "@id": "http://example.com/alice"
}

const aliceAtRoot = jsonld.frame(newDocument, aliceFrame);



/*
const jsonpatch = require('fast-json-patch');

const document = {
  "@context": "http://schema.org/",
  "@type": "Person",
  "name": "Alice",
};

const observer = jsonpatch.observe(document);

document.email = 'alice@example.com';
const patch = jsonpatch.generate(observer);
jsonpatch.unobserve(document, observer);

console.log('patch', patch);
*/

```

## API Documentation

### Apply a JSON patch

### Generate a JSON patch

A JSON patch can be generated by observing changes to a document.

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