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
- Host: GitHub
- URL: https://github.com/digitalbazaar/jsonld-patch
- Owner: digitalbazaar
- License: bsd-3-clause
- Created: 2018-03-30T14:13:08.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2021-02-25T19:39:34.000Z (over 5 years ago)
- Last Synced: 2025-05-02T14:21:36.184Z (about 1 year ago)
- Language: JavaScript
- Size: 25.4 KB
- Stars: 7
- Watchers: 13
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
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.