How to convert JSON array to RDF in Java?

前端 未结 2 1786
眼角桃花
眼角桃花 2020-12-20 07:19

I want to convert a JSON (not JSON-LD) array to RDF in Java. I have seen similar posts to the forum but not exact answer. The JSON array contains objects and arrays, somethi

2条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-20 08:04

    You could define a proper @context and use any JSON-LD converter to create any RDF serialization. Find Java example here and here.

    Example

    {
    "@context":{
    "results":{
      "@id":"info:stack/49365220/results",
      "@container":"@list"
    },
    "record_id":{
      "@id":"info:stack/49365220/record_id"
    }, 
    "gender":{
      "@id":"info:stack/49365220/gender"
    }, 
    "age":{
      "@id":"info:stack/49365220/age"
    },
    "demographics":{
      "@id":"info:stack/49365220/demographics"
    }
    },
    "results": [
      {
         "record_id": "3d87f4df-f17e-4632-9449",
         "demographics": { "gender":"female", "race":"", "age":20 }
      },
      {
         "record_id": "ec5ca92d-865a-431f-9984",
         "demographics": { "gender":"male", "age":118 }
      },
      {
         "record_id": "0a79ecf0-83d8-4148-9054",
         "demographics": { "gender":"female", "age":118 }
      },
      {
         "record_id": "229276f8-1893-480b-b6e7",
         "demographics": { "gender":"female", "age":35 }
      },
      {
         "record_id": "0574cc3b-fb9c-495f-851c",
         "demographics": { "gender":"female", "age":40 }
      },
      {
         "record_id": "f3ccfdf6-231e-4a3e-bee0",
         "demographics": { "gender":"male", "age":118 }
      }
       ]
    }
    

    When putting this to Json-Ld Playground (or any other RDF tool) you can produce something like this:

    serialized as N-TRIPLE

    _:b0  _:b13 .
    _:b1  _:b2 .
    _:b1  "3d87f4df-f17e-4632-9449" .
    _:b10  "40"^^ .
    _:b10  "female" .
    _:b11  _:b12 .
    _:b11  "f3ccfdf6-231e-4a3e-bee0" .
    _:b12  "118"^^ .
    _:b12  "male" .
    _:b13  _:b1 .
    _:b13  _:b14 .
    _:b14  _:b3 .
    _:b14  _:b15 .
    _:b15  _:b5 .
    _:b15  _:b16 .
    _:b16  _:b7 .
    _:b16  _:b17 .
    _:b17  _:b9 .
    _:b17  _:b18 .
    _:b18  _:b11 .
    _:b18   .
    _:b2  "20"^^ .
    _:b2  "female" .
    _:b3  _:b4 .
    _:b3  "ec5ca92d-865a-431f-9984" .
    _:b4  "118"^^ .
    _:b4  "male" .
    _:b5  _:b6 .
    _:b5  "0a79ecf0-83d8-4148-9054" .
    _:b6  "118"^^ .
    _:b6  "female" .
    _:b7  _:b8 .
    _:b7  "229276f8-1893-480b-b6e7" .
    _:b8  "35"^^ .
    _:b8  "female" .
    _:b9  _:b10 .
    _:b9  "0574cc3b-fb9c-495f-851c" .
    

提交回复
热议问题