{"id":14976214,"url":"https://github.com/vsimko/jsonld-object-graph","last_synced_at":"2025-10-27T18:30:47.574Z","repository":{"id":33629289,"uuid":"138061627","full_name":"vsimko/jsonld-object-graph","owner":"vsimko","description":"Convert JSON-LD to Javascript object-graph","archived":false,"fork":false,"pushed_at":"2023-01-05T14:41:09.000Z","size":632,"stargazers_count":11,"open_issues_count":8,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-01T07:31:51.811Z","etag":null,"topics":["graphql","javascript","javascript-library","json-ld","linked-data","rdf","semantic-web"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vsimko.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-06-20T16:56:58.000Z","updated_at":"2023-09-08T08:19:57.000Z","dependencies_parsed_at":"2023-01-15T01:43:56.449Z","dependency_job_id":null,"html_url":"https://github.com/vsimko/jsonld-object-graph","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vsimko%2Fjsonld-object-graph","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vsimko%2Fjsonld-object-graph/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vsimko%2Fjsonld-object-graph/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vsimko%2Fjsonld-object-graph/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vsimko","download_url":"https://codeload.github.com/vsimko/jsonld-object-graph/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238536123,"owners_count":19488661,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["graphql","javascript","javascript-library","json-ld","linked-data","rdf","semantic-web"],"created_at":"2024-09-24T13:53:31.226Z","updated_at":"2025-10-27T18:30:47.254Z","avatar_url":"https://github.com/vsimko.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"![](img/logo.svg)\n\n[![Build Status](https://travis-ci.org/vsimko/jsonld-object-graph.svg?branch=master)](https://travis-ci.org/vsimko/jsonld-object-graph)\n[![Known Vulnerabilities](https://snyk.io/test/github/vsimko/jsonld-object-graph/badge.svg?targetFile=package.json)](https://snyk.io/test/github/vsimko/jsonld-object-graph?targetFile=package.json)\n\nThe function `jsonld2obj` constructs an object graph in memory by resolving the `@id` properties recursively.\nThe graph can contain cycles. This is handy if you want to navigate graphs represented in RDF (as json-ld) from javascript code.\n\n# Installation\n```sh\n# for yarn users\nyarn add jsonld-object-graph\n\n# for npm users\nnpm install jsonld-object-graph\n```\n**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](.travis.yml) file).\n\n# TL;DR\n```js\nconst data = ... get JSON-LD data from somewhere ...\nconst {jsonld2obj, autoSimplifier, mutateGraphKeys} = require(\"jsonld-object-graph\")\nconst graph = await jsonld2obj(data)\nmutateGraphKeys(autoSimplifier)(graph)\n\ngraph.Gordon.knows.Alyx.name // -\u003e \"Alyx Vence\"\ngraph.Gordon.knows.Alyx.knows.name // -\u003e \"Gordon Freeman\"\n```\n\n# Example\n```js\nconst {jsonld2obj} = require(\"jsonld-object-graph\")\nconst data = [\n  {\n    \"@context\": {\n      \"@base\": \"http://halflife/\",\n      \"@vocab\": \"http://schema.org/\",\n      \"knows\": {\n        \"@type\" :\"@id\"\n      }\n    },\n    \"@id\": \"Gordon\",\n    \"@type\": \"Person\",\n    \"gender\": \"male\",\n    \"name\": \"Gordon Freeman\",\n    \"knows\": [\n      {\n        \"@id\": \"Alyx\",\n        \"@type\": [\"Person\", \"Hacker\"],\n        \"gender\": \"female\",\n        \"name\": \"Alyx Vence\",\n        \"knows\": \"Gordon\",\n        \"owns\": \"Pistol\",\n      },\n      \"Barney\"\n    ],\n    \"owns\": [\"Gravity Gun\", \"Crowbar\"]\n  }\n]\n\nconst graph = await jsonld2obj(data)\nconsole.log(graph)\n```\n\nGenerates the following output:\n```\n{ 'http://halflife/Gordon':\n   { 'http://halflife/Gordon': [Circular],\n   ...\n   ...\n}\n```\n\nNow it is possible to navigate the graph as follows:\n```js\ngraph\n  [\"http://halflife/Alyx\"]\n  [\"http://schema.org/knows\"]\n  [\"http://schema.org/name\"] // -\u003e \"Gordon Freeman\"\n\ngraph\n  [\"http://halflife/Alyx\"]\n  [\"http://schema.org/knows\"]\n  [\"http://halflife/Gordon\"]\n  [\"http://schema.org/name\"] // -\u003e \"Gordon Freeman\"\n```\n\n## Shorteninig of property names\n\nOf course, we don't like these huge identifiers in our code.\nTo shorten the property names, such as `http://schema.org/knows` to `knows`, we can use the following function:\n```js\nconst {autoSimplifier, mutateGraphKeys} = require(\"jsonld-object-graph\")\nmutateGraphKeys(autoSimplifier)(graph) // mutates the graph in-place\n```\n\nNow it is possible to navigate the graph as follows:\n```js\ngraph.Gordon.knows.Alyx.name // -\u003e \"Alyx Vence\"\ngraph.Gordon.knows.Alyx.knows.name // -\u003e \"Gordon Freeman\"\n```\n\nThe function `mutateGraphKeys` is not pure, it mutates the keys in the original graph.\nDuring this process, an exception is thrown if an ambigous replacement has occured.\n\n## Other replacement functions\n\n- `dropPrefix` : removes prefix from each key\n- `dropNsPrefix` : removes namespace prefix, e.g. `schema:`\n- `toUnderscores` : replaces special characters to `_` to make navigation in javascript easier\n- `afterLastSlash` : keeps the word after the last slash e.g. `http://schema.org/known` to `knows`\n- `afterLastHash` : e.g. `http://something#abc` to `abc`\n- `autoSimplifier` : combines multiple operations\n\n## Replacements in a functional way\n\nWith ramda (or sanctuary) we can compose the replacements in a functional way as follows:\n```js\nconst {compose, replace} = require(\"ramda\")\nconst replacers = compose(\n  dropNsPrefix(\"foaf\"),\n  dropNsPrefix(\"schema\"),\n  replace(/...regex.../, \"...\"), // custom regex replacement\n  /* ... */\n)\n```\n\n## Types resolved automagically\n\nIf 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)\n\n```js\ngraph.Gordon.$type // -\u003e Multival(Person)\ngraph.Alyx.$type // -\u003e Multival(Person, Hacker)\n```\n\n## Configuration\n\nSince v1.0.0, there is `defaultConfig` with sensible configuration parameters that can be customized as follows:\n```js\nconst { jsonld2objWithConfig, defaultConfig } = require(\"jsonld-object-graph\")\nconst myConfig = { ...defaultConfig, addSelfRef:false }\nconst jsonld2obj = jsonld2objWithConfig(myConfig)\n```\n\nThe following param can be configured:\n- **addSelfRef** (default `true`) whether to add self-reference e.g. `graph.Alyx.Alyx == graph.Alyx`\n- **addTypeRef** (default `true`) whether to add the resolved type object as an reference to its instance \n- **shouldResolveTypeObjects** (default `true`) whether to resolve the \"@type\" as an object\n- **idFieldName** (default `\"$id\"`) how the \"@id\" field should be renamed\n- **typeFieldName** (default `\"$type\"`) how the \"@type\" field should be renamed\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvsimko%2Fjsonld-object-graph","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvsimko%2Fjsonld-object-graph","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvsimko%2Fjsonld-object-graph/lists"}