JSON-LD to normal JSON

后端 未结 1 1970
独厮守ぢ
独厮守ぢ 2021-01-24 15:24

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

相关标签:
1条回答
  • 2021-01-24 16:00

    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 .

    0 讨论(0)
提交回复
热议问题