定义JSON-LD @context以加入/拆分值?

时间:2015-10-21 22:03:58

标签: javascript json schema.org json-ld linked-data

我想使用jsonld.js库的expandcompact方法将各种来源的数据转换为常用的处理格式。如果我获取源JSON文档,请向其添加@context,然后通过expand方法传递它,以便我能够获得所需的通用格式。

我无法找到解决方案的用例是需要合并多个值时。例如,schema.org为PostalAddress定义了一个streetAddress字段,但许多系统将街道地址存储为单独的值(街道号,街道名称,街道方向......)。要将传入的数据转换为schema.org格式,我需要一种方法在@context中指出多个字段以正确的顺序组成streetAddress

压缩文档

{
    "@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"
}

扩展文档

{
   "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/"
      }
   ]
}

我已经查看了我能找到的所有JSON-LD规范,并且无法找到任何指示使用@context拆分或连接值的方法。

是否有人知道以正确的顺序将多个值映射到一个上下文属性的方法,并且可能在值之间添加空格。我还需要找到一个反向场景的解决方案,我需要按正确的顺序将一个字段拆分成多个值。

注意:即使我将所有三个属性都映射到streetAddress,这些值都将包含在数组中,但不能保证它们的顺序正确

2 个答案:

答案 0 :(得分:3)

实现此目的的一种可能方法是对包含有序地址组件的地址使用单个数组字段(即["number", "direction", "name"])。然后在@context中,您可以使用address指定@container: @list,这将确保正确排序地址组件。

因此,压缩文件将是:

{
    "@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"]
}

扩展的那个将是

  {
    "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/"
      }
    ]
  }

答案 1 :(得分: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