Skip to content

CodeJamboree/json-ld

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JSON-LD

This is a class to retrieve data from JSON Linked Data (JSON-LD) files.

Basic usage

import JsonLD from "@codejamboree/json-ld";
import fs from 'fs';

const data = {
  '@graph': [
    {
      '@id': '1',
      '@value': 'Hello World',
      'foo': 'bar',
      'bar': { '@id': '2'}
    },
    {
      '@id': '2',
      '@value': 'none',
      'code': {
        '@value': 'scanner'
      }
    }
  ]
};
jsonld = new JsonLD(data, 'en');

console.log(jsonld.getValue('1'));
// Hello World
console.log(jsonld.getValue('1', 'foo'));
// bar
console.log(jsonld.getValue('1', 'bar'));
// none
console.log(jsonld.getValue('1', 'bar', 'code'));
// scanner

Languages

A value may different based on the language being accessed. If a language is not present, then the default behavior resolves to English.

const data = {
  '@graph': [
    {
      '@id': 'greetings',
      'hello': [
        {'@language': 'en', '@value': 'Hello'},
        {'@language': 'fr', '@value': 'Bojour'},
      ]
    }
  ]
};
jsonld = new JsonLD(data, 'en');
console.log(jsonld.getValue('greetings', 'hello'));
// Hello
jsonld.setLanguage('fr');
console.log(jsonld.getValue('greetings', 'hello'));
// Bonjour
jsonld.setLanguage('es');
console.log(jsonld.getValue('greetings', 'hello'));
// Hello

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published