Serializing json to rdf with java

I have a json object:

[{
"id": 1,
"text": "Item 1",
"iconCls": "icon-ok",
"target": {
    "jQuery180016273543753015074": 16
},
"checked": false,
"state": "open",
"children": [{
    "id": 2,
    "text": "Item 1_1",
    "target": {
        "jQuery180016273543753015074": 15
    },
    "checked": false,
    "state": "open",
    "children": [{
        "id": 3,
        "text": "Item 1_1_1",
        "target": {
            "jQuery180016273543753015074": 14
        },
        "checked": false,
        "state": "open",
        "children": [{
            "id": 7,
            "text": "Item 1_1_1_1",
            "target": {
                "jQuery180016273543753015074": 13
            },
            "checked": false
        },
        {
            "id": 6,
            "text": "Item 1_1_1_2",
            "target": {
                "jQuery180016273543753015074": 12
            },
            "checked": false
        }]
    }]
},
{
    "id": 8,
    "text": "Item 1_1_2",
    "target": {
        "jQuery180016273543753015074": 11
    },
    "checked": false,
    "state": "open",
    "children": [{
        "id": 4,
        "text": "Item 1_1_2_1",
        "target": {
            "jQuery180016273543753015074": 10
        },
        "checked": false
    },
    {
        "id": 5,
        "text": "Item 1_1_2_2",
        "target": {
            "jQuery180016273543753015074": 9
        },
        "checked": false
    }]
},
{
    "id": 9,
    "text": "Item 1_1_3",
    "target": {
        "jQuery180016273543753015074": 17
    },
    "checked": false
}]
 }]

      

and I need to use java to serialize it in RDF ontology which will keep the hierarchy of children as a "subclass" property. Can anyone suggest an efficient algorithm for parsing JSON? I am using the internal Java API to handle the ontology, so the algorithm instead of the code would be more useful in this case.

+3


source to share


1 answer


You can use any of the libraries listed here to parse your JSON and Jena document to create RDF triplets.

You probably want to recursively traverse the JSON document and for each node create an RDF node with as many attributes as JSON properties that are stored in the node. To represent child relationships you can use rdfs:subClassOf

, so node 2 will be rdfs:subClassOf

node 1.

This is an example of how node 1 and node 2 are serialized in RDF / Turtle :



   @prefix : <http://other.example.org/ns#> .

   :node_1 rdf:type :Node;
    :text "Item 1";
    :iconCls 16;
    :target [
        :jQueryID "180016273543753015074";
        :number 11;
    ];
    :checked false;
    :state "open" .

  :node_2 rdf:type :Node;
    :text "Item 1";
    :iconCls 16;
    :target [
        :jQueryID "180016273543753015074";
        :number 11;
    ];
    :checked false;
    :state "open";
    rdfs:subClassOf :node_1 .

      

You might need to look at the Turtle spec document to understand how this happens, but it's intuitive anyway. Keep in mind that there are several ways to serialize RDF triggers, the most readable is RDF / Turtle. As you can see the child relationship between nodes is registered in node 2.

+3


source







All Articles