JSON Operating System - A lightweight framework for building decentralized applications with JSON-LD
npm install jsonos
Or use directly in the browser:
<script type="module">
import { JsonOS } from 'https://esm.sh/jsonos';
</script>
import { JsonOS } from 'jsonos';
// Initialize with your storage endpoint
const os = new JsonOS({
storage: 'https://pod.example/data/'
});
// Read JSON-LD data
const todos = await os.read('/todos.json');
console.log(todos);
// Each item has a computed _view layer
todos.forEach(todo => {
console.log(todo._view.title); // Normalized title
console.log(todo._view.completed); // Boolean completion status
});
// Write data back
await os.write('/todos.json', todos);
new JsonOS(options)Create a new JsonOS instance.
Options:
storage - Base URL for storage operationsfetch - Custom fetch function (for authentication)prefixes - Additional prefix mappings for predicate expansionos.read(path)Read JSON-LD data from a path. Returns an array with view layer attached.
const data = await os.read('/tasks.json');
// Returns: [{ ...originalData, _view: { title, completed, ... }, _format: 'schema' }]
os.write(path, data)Write JSON-LD data. Automatically strips view layer before saving.
await os.write('/tasks.json', tasks);
os.delete(path)Delete a resource.
await os.delete('/tasks.json');
os.extractValue(item, ...predicates)Extract a value from an item using multiple predicate attempts. Handles JSON-LD value objects and prefix expansion.
const title = os.extractValue(item, 'dc:title', 'dct:title', 'rdfs:label');
os.detectFormat(data)Detect the format of JSON-LD data. Returns 'schema', 'wf', or 'unknown'.
os.clearCache()Clear the internal read cache.
jsonos uses the “Preserve + Layer” pattern to handle different RDF formats:
// Original data is preserved
{
"@id": "#task1",
"@type": ["wf:Open", "wf:Task"],
"dc:title": "Fix bug",
"dct:created": "2024-01-15",
// Computed view layer added
_view: {
title: "Fix bug",
completed: false,
created: "2024-01-15"
},
_format: "wf"
}
This allows you to:
| Format | Detection | Read | Write |
|---|---|---|---|
| Schema.org (Task/Tracker) | ✅ | ✅ | ✅ |
| wf: Workflow Ontology | ✅ | ✅ | ❌* |
| Unknown JSON-LD | ✅ | ✅ | ✅ |
*wf: format is read-only to preserve original semantics
import { JsonOS } from 'jsonos';
import { getDefaultSession } from '@inrupt/solid-client-authn-browser';
// After authentication
const session = getDefaultSession();
const os = new JsonOS({
storage: session.info.webId.replace('/profile/card#me', '/'),
fetch: session.fetch
});
// Now reads/writes are authenticated
const todos = await os.read('/public/todos.json');
jsonos expands these prefixes automatically:
| Prefix | URI |
|---|---|
dc: |
http://purl.org/dc/elements/1.1/ |
dct: |
http://purl.org/dc/terms/ |
rdfs: |
http://www.w3.org/2000/01/rdf-schema# |
schema: |
http://schema.org/ |
wf: |
http://www.w3.org/2005/01/wf/flow# |
solid: |
http://www.w3.org/ns/solid/terms# |
Add custom prefixes:
const os = new JsonOS({
prefixes: {
'my:': 'https://example.org/vocab#'
}
});
See the examples directory for complete working examples:
AGPL-3.0-or-later