Search code examples
javascriptjsonschema.orgjson-ldlinked-data

Define JSON-LD @context to join/split values?


I'd like to use the expand and compact methods of the jsonld.js library to translate data from various sources into a common format for processing. If I take a source JSON document, add a @context to it, then pass it through the expand method I'm able to get the common format that I need.

The use case that I haven't been able to find a solution for is when multiple values need to be merged. For example, schema.org defines a PostalAddress with a single field for the streetAddress, but many systems store the street address as separate values (street number, street name, street direction...). To translate the incoming data to the schema.org format I need a way to indicate in my @context that multiple fields make up the streetAddress, in the correct order.

Compacted Document

{
    "@context": {
        "displaName": "http://schema.org/name",
        "website": "http://schema.org/homepage",
        "icon": "http://schema.org/image",
        "streetNumber": "http://schema.org/streetAddress"
    },
    "displaName": "John Doe",
    "website": "http://example.com/",
    "icon": "http://example.com/images/test.png",
    "streetNumber": "123",
    "streetName": "Main St",
    "streetDirection": "South"
}

Expanded Document

{
   "http://schema.org/name":[
      {
         "@value":"John Doe"
      }
   ],
   "http://schema.org/image":[
      {
         "@value":"http://example.com/images/test.png"
      }
   ],
   "http://schema.org/streetAddress":[
      {
         "@value":"123"
      }
   ],
   "http://schema.org/homepage":[
      {
         "@value":"http://example.com/"
      }
   ]
}

I've reviewed all of the JSON-LD specs that I could find and haven't been able to locate anything that indicates a way to split or concatenate values using the @context.

Is anyone aware of a way to map multiple values into one context property, in the correct order, and possibly add whitespace between the values. I also need to find a solution for the reverse scenario, where I need to split one field into multiple values, in the correct order.

Note: Even if I map all three properties to streetAddress, the values will all be included in the array, but there's no guarantee they'll be in the correct order.


Solution

  • I posted an issue on the jsonld.js Github repository. According to @dlongley, the original creator of the jsonld.js library, it's not possible to manipulate properties in this manor, using standard JSON-LD.

    https://github.com/digitalbazaar/jsonld.js/issues/115