JSON-LD to normal JSON

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-20 04:11:21

问题


I'm trying to insert a JSON-LD file into my CouchDB. The only problem I have is that when I insert my JSON-LD file, the resulting CouchDB is meaningless because the IDs aren't linked together.

An example of what my JSON-LD file looks like:

"contributor": [
{
    "@id": "_:N6e57c55b35b74782ada714fdc6d66bf1"
},
{
    "@id": "_:N810e115dfb3348579a7b826a7548095b"
}

And another part:

{
  "@id": "_:N6e57c55b35b74782ada714fdc6d66bf1",
  "@type": "Person",
  "label": "Isely, Duane, 1918-"
},

{
  "@id": "_:N810e115dfb3348579a7b826a7548095b",
  "@type": "Person",
  "label": "Cronquist, Arthur"
}

Now the IDs in "contributor" are linking to the two fields of the second part, which is describing person. I would like to know how to link them (the correct way), so I would get something like this:

"contributor": [
{
      "@type": "Person",
      "label": "Isely, Duane, 1918-"
},
{
      "@type": "Person",
      "label": "Cronquist, Arthur"
}

回答1:


You could use a JSON-LD processor to (re)create nicer JSON for your DB. A good possibility to prescribe the structure of JSON-LD documents is to define a frame.

Quote from spec:

JSON-LD Framing allows developers to query by example and force a specific tree layout to a JSON-LD document.

Example:

Assuming your document looks like

{
  "@context": {
    "contributor": {
      "@type": "@id",
      "@id": "http://purl.org/dc/terms/contributor",
      "@container": "@list"
    },
    "label": {
      "@id": "http://www.w3.org/2004/02/skos/core#prefLabel"
    }
  },
  "@graph": [
    {
      "@type": "MainResource",
      "@id": "_:foo",
      "contributor": [
        {
          "@id": "_:N6e57c55b35b74782ada714fdc6d66bf1"
        },
        {
          "@id": "_:N810e115dfb3348579a7b826a7548095b"
        }
      ]
    },
    {
      "@id": "_:N6e57c55b35b74782ada714fdc6d66bf1",
      "@type": "Person",
     "label": "Isely, Duane, 1918-"
    },
    {
      "@id": "_:N810e115dfb3348579a7b826a7548095b",
      "@type": "Person",
      "label": "Cronquist, Arthur"
    }
  ]
}

Add a JSON-LD Frame like

{
  "@context": {
    "contributor": {
      "@type": "@id",
      "@id": "http://purl.org/dc/terms/contributor",
      "@container": "@list"
    },
    "label": {
      "@id": "http://www.w3.org/2004/02/skos/core#prefLabel"
    }
  },
  "@type": "MainResource",
  "@embed": "always"
}

Throw it to a JSON-LD processor of your choice and you will get something like

{
  "@context": {
    "contributor": {
      "@type": "@id",
      "@id": "http://purl.org/dc/terms/contributor",
      "@container": "@list"
    },
    "label": {
      "@id": "http://www.w3.org/2004/02/skos/core#prefLabel"
    }
  },
  "@graph": [
    {
      "@id": "_:b0",
     "@type": "http://json-ld.org/playground/MainResource",
      "contributor": [
        {
          "@id": "_:b1",
          "@type": "http://json-ld.org/playground/Person",
          "label": "Isely, Duane, 1918-"
        },
        {
          "@id": "_:b2",
          "@type": "http://json-ld.org/playground/Person",
          "label": "Cronquist, Arthur"
        }
      ]
    }
  ]
}

Here is the complete example in json-ld.org/playground

Unfortunately framing is not equaly well supported. So the result depends on the JSON-LD processor you're using.

You can elaborate further by removing "@" signs from your data. Simply add the following to your context:

"type" : "@type",
"id" :"@id"

Also, you can add shortenings for types to your context document

"MainResource": "http://json-ld.org/playground/MainResource"

See example in json-ld.org/playground

For full code java example with rdf4j look here: How to convert RDF to pretty nested JSON using java rdf4j .



来源:https://stackoverflow.com/questions/42398060/json-ld-to-normal-json

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!