An open API service indexing awesome lists of open source software.

https://github.com/digitalbazaar/jsonld-patch

JSON patch for JSON-LD
https://github.com/digitalbazaar/jsonld-patch

Last synced: about 1 year ago
JSON representation

JSON patch for JSON-LD

Awesome Lists containing this project

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.