简体   繁体   English

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

[英]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. 我想使用jsonld.js库的expandcompact方法将来自各种源的数据转换为通用格式进行处理。 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. 如果我获取源JSON文档,向它添加一个@context ,然后通过expand方法传递它我能够获得我需要的通用格式。

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...). 例如,schema.org限定PostalAddress与用于单个字段streetAddress ,但许多系统存储所述街道地址作为单独的值(街道号,街道名称,街道方向...)。 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. 要将传入的数据转换为schema.org格式,我需要一种方法在我的@context中指示多个字段以正确的顺序组成streetAddress

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 . 我已经回顾了我能找到的所有JSON-LD规范,但是找不到任何指示使用@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. 注意:即使我将所有三个属性映射到streetAddress ,这些值都将包含在数组中,但不能保证它们的顺序正确。

One possible way to achieve this is to use a single array field for your address containing the ordered address components (ie ["number", "direction", "name"] ). 实现此目的的一种可能方法是对包含有序地址组件的地址使用单个数组字段(即["number", "direction", "name"] )。 Then in the @context you can specify the address with @container: @list , which will ensure the address components are correctly ordered. 然后在@context您可以使用@container: @list指定address ,这将确保正确排序地址组件。

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

I posted an issue on the jsonld.js Github repository. 我在jsonld.js Github存储库上发布了一个问题。 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. 根据jsonld.js库的原始创建者@dlongley,使用标准JSON-LD无法操纵此庄园中的属性。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM