13

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.

Brian Shamblen
  • 4,653
  • 1
  • 23
  • 37

2 Answers2

3

One possible way to achieve this is to use a single array field for your address containing the ordered address components (i.e. ["number", "direction", "name"]). Then in the @context you can specify the address with @container: @list, which will ensure the address components are correctly ordered.

So the compacted document would be:

{
    "@context": {
        "displaName": "http://schema.org/name",
        "website": "http://schema.org/homepage",
        "icon": "http://schema.org/image",
        "address": {
          "@id": "http://schema.org/streetAddress",
          "@container": "@list"
        }
    },
    "displaName": "John Doe",
    "website": "http://example.com/",
    "icon": "http://example.com/images/test.png",
    "address": ["123", "South", "Main St"]
}

And the expanded one would be

  {
    "http://schema.org/streetAddress": [
      {
        "@list": [
          {
            "@value": "123"
          },
          {
            "@value": "South"
          },
          {
            "@value": "Main St"
          }
        ]
      }
    ],
    "http://schema.org/name": [
      {
        "@value": "John Doe"
      }
    ],
    "http://schema.org/image": [
      {
        "@value": "http://example.com/images/test.png"
      }
    ],
    "http://schema.org/homepage": [
      {
        "@value": "http://example.com/"
      }
    ]
  }
Val
  • 207,596
  • 13
  • 358
  • 360
  • thanks for the answer. Unfortunately I need to keep the original document in tact. The purpose of the question is to find a way to handle the translation using the context, assuming I'm not the one providing the input data. – Brian Shamblen Oct 25 '15 at 06:08
  • Ok, I see, sorry for the misunderstanding. I might know of another way, let me check. Even though the fields must be left intact, do you have full control over the `@context`? – Val Oct 25 '15 at 06:09
  • Yes. I'm trying to create a way to allow users to map API outputs into a common format. I already have a working UI for allowing a one to one mapping, but have not been able to figure out from the documentation of the json-ld processor how to manipulate data within the source document, beyond mapping the keys to their respective vocabulary. – Brian Shamblen Oct 25 '15 at 06:23
1

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

Brian Shamblen
  • 4,653
  • 1
  • 23
  • 37