Search code examples
json-ld

Contextualize expanded json-ld for a given @context


The expansion algorithm https://www.w3.org/TR/json-ld-api/#expansion expands a json-ld document using its @context.

Is there also a "reverse" algorithm, which, given an expanded json-ld document and a context definition (IRI or document), will generate json-ld where the absolute IRIs, blank node identifiers, or keywords are "compacted" according to the context definitions?

I can't find it in https://github.com/jsonld-java/jsonld-java or on json-ld.org/spec/latest/json-ld-api/


Solution

  • The Compaction Algorithm in JSON-LD API does much of that. Non-document relative IRIs are compacted to terms, or prefixed names consistent with the definitions in your context. For document-relative IRIs (such as the value of @id), if you include @base within the context, IRIs will be made relative to that base. To make IRIs relative without hard-coding @base in your context. Processors may include a mechanism to pass a base into the compaction algorithm to do this without using @base; I can't say what jsonld-java does. In the Ruby JSON-LD gem, the compact algorithm accepts a base option for doing this.