npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jsonld-object-graph

v1.0.9

Published

Working with json-ld as in memory object-graph

Downloads

20

Readme

Build Status Known Vulnerabilities

The function jsonld2obj constructs an object graph in memory by resolving the @id properties recursively. The graph can contain cycles. This is handy if you want to navigate graphs represented in RDF (as json-ld) from javascript code.

Installation

# for yarn users
yarn add jsonld-object-graph

# for npm users
npm install jsonld-object-graph

Note: This library uses jsonld package as a dependency, which at some point depends on some native C code which needs to be compiled through gyp. Make sure you can compile native code on your platform. We build our package on Travis-CI, so you can take a look, how the build environment is configured (see .travis.yml file).

TL;DR

const data = ... get JSON-LD data from somewhere ...
const {jsonld2obj, autoSimplifier, mutateGraphKeys} = require("jsonld-object-graph")
const graph = await jsonld2obj(data)
mutateGraphKeys(autoSimplifier)(graph)

graph.Gordon.knows.Alyx.name // -> "Alyx Vence"
graph.Gordon.knows.Alyx.knows.name // -> "Gordon Freeman"

Example

const {jsonld2obj} = require("jsonld-object-graph")
const data = [
  {
    "@context": {
      "@base": "http://halflife/",
      "@vocab": "http://schema.org/",
      "knows": {
        "@type" :"@id"
      }
    },
    "@id": "Gordon",
    "@type": "Person",
    "gender": "male",
    "name": "Gordon Freeman",
    "knows": [
      {
        "@id": "Alyx",
        "@type": ["Person", "Hacker"],
        "gender": "female",
        "name": "Alyx Vence",
        "knows": "Gordon",
        "owns": "Pistol",
      },
      "Barney"
    ],
    "owns": ["Gravity Gun", "Crowbar"]
  }
]

const graph = await jsonld2obj(data)
console.log(graph)

Generates the following output:

{ 'http://halflife/Gordon':
   { 'http://halflife/Gordon': [Circular],
   ...
   ...
}

Now it is possible to navigate the graph as follows:

graph
  ["http://halflife/Alyx"]
  ["http://schema.org/knows"]
  ["http://schema.org/name"] // -> "Gordon Freeman"

graph
  ["http://halflife/Alyx"]
  ["http://schema.org/knows"]
  ["http://halflife/Gordon"]
  ["http://schema.org/name"] // -> "Gordon Freeman"

Shorteninig of property names

Of course, we don't like these huge identifiers in our code. To shorten the property names, such as http://schema.org/knows to knows, we can use the following function:

const {autoSimplifier, mutateGraphKeys} = require("jsonld-object-graph")
mutateGraphKeys(autoSimplifier)(graph) // mutates the graph in-place

Now it is possible to navigate the graph as follows:

graph.Gordon.knows.Alyx.name // -> "Alyx Vence"
graph.Gordon.knows.Alyx.knows.name // -> "Gordon Freeman"

The function mutateGraphKeys is not pure, it mutates the keys in the original graph. During this process, an exception is thrown if an ambigous replacement has occured.

Other replacement functions

  • dropPrefix : removes prefix from each key
  • dropNsPrefix : removes namespace prefix, e.g. schema:
  • toUnderscores : replaces special characters to _ to make navigation in javascript easier
  • afterLastSlash : keeps the word after the last slash e.g. http://schema.org/known to knows
  • afterLastHash : e.g. http://something#abc to abc
  • autoSimplifier : combines multiple operations

Replacements in a functional way

With ramda (or sanctuary) we can compose the replacements in a functional way as follows:

const {compose, replace} = require("ramda")
const replacers = compose(
  dropNsPrefix("foaf"),
  dropNsPrefix("schema"),
  replace(/...regex.../, "..."), // custom regex replacement
  /* ... */
)

Types resolved automagically

If your JSON-LD data contains the @type property, our function automatically resolves it into a javascript object and makes it accessible through $type property (for easier navigation in javascript)

graph.Gordon.$type // -> Multival(Person)
graph.Alyx.$type // -> Multival(Person, Hacker)

Configuration

Since v1.0.0, there is defaultConfig with sensible configuration parameters that can be customized as follows:

const { jsonld2objWithConfig, defaultConfig } = require("jsonld-object-graph")
const myConfig = { ...defaultConfig, addSelfRef:false }
const jsonld2obj = jsonld2objWithConfig(myConfig)

The following param can be configured:

  • addSelfRef (default true) whether to add self-reference e.g. graph.Alyx.Alyx == graph.Alyx
  • addTypeRef (default true) whether to add the resolved type object as an reference to its instance
  • shouldResolveTypeObjects (default true) whether to resolve the "@type" as an object
  • idFieldName (default "$id") how the "@id" field should be renamed
  • typeFieldName (default "$type") how the "@type" field should be renamed