Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert JSON file into RDF format using Java

Tags:

java

json

rdf

Here I want to convert the JSON file into RDF. Here is the JSON

{
"glossary": {
    "title": "example glossary",
    "GlossDiv": {
        "title": "S",
        "GlossList": {
            "GlossEntry": {
                "ID": "SGML",
                "SortAs": "SGML",
                "GlossTerm": "Standard Generalized Markup Language",
                "Acronym": "SGML",
                "Abbrev": "ISO 8879:1986",
                "GlossDef": {
                    "para": "A meta-markup language, used to create markup languages such as DocBook.",
                    "GlossSeeAlso": ["GML", "XML"]
                },
                "GlossSee": "markup"
            }
        }
    }
}
}

I could not find a proper way to convert it into RDF.

like image 271
Kalanka Avatar asked Dec 03 '22 12:12

Kalanka


2 Answers

There is no standard way to interpret JSON as RDF. There are several ways you can generate RDF from a JSON file though (in Java or otherwise). You could simply use a JSON parser implemented in Java, then extract the relevant parts and build an RDF graph using a Java library for RDF such as Apache Jena or RDF4J (formerly known as Sesame). However, there are other ways that could make the task much easier:

  • Transform the JSON file into a JSON-LD file by adding a @context to it. This works well for simple cases but is not sufficient to cover many relevant cases.
  • Use RML, a language for expressing mappings from various data formats (including JSON) to RDF. It has a reference implementation in Java. RML is an extension of R2RML, so it can also map relational data to RDF, and if you are familiar with R2RML, it is relatively easy to understand how RML works. There is also a graphical editor, but it seems it is not available for download.
  • Use SPARQL-Generate, a language for expressing mappings from non-RDF data sources (including JSON) to RDF. It has a reference implementation based on Jena. It extends SPARQL, so if you are familiar with SPARQL, it should be quite easy to use it. It can be tested online.

Disclaimer: I contributed to SPARQL-Generate.

like image 78
Antoine Zimmermann Avatar answered Dec 22 '22 15:12

Antoine Zimmermann


If your aim is to simply get valid RDF without making any decisions about structure, you could trivially add a @context object and turn the existing JSON into JSON-LD, e.g.

{
  "@context": {"@vocab": "http://example.org/ontology#"},
  "glossary": {
    "title": "example glossary",
    "GlossDiv": {
        "title": "S",
        "GlossList": {
            "GlossEntry": {
                "ID": "SGML",
                "SortAs": "SGML",
                "GlossTerm": "Standard Generalized Markup Language",
                "Acronym": "SGML",
                "Abbrev": "ISO 8879:1986",
                "GlossDef": {
                    "para": "A meta-markup language, used to create markup languages such as DocBook.",
                    "GlossSeeAlso": ["GML", "XML"]
                },
                "GlossSee": "markup"
            }
        }
    }
  }
}

This can then be interpreted as RDF using an appropriate RDF/JSON-LD library, the RDF structure would be:

@prefix ns0: <http://example.org/ontology#> .

[] ns0:glossary [
    ns0:GlossDiv [
      ns0:GlossList [ ns0:GlossEntry [
          ns0:Abbrev "ISO 8879:1986" ;
          ns0:Acronym "SGML" ;
          ns0:GlossDef [
            ns0:GlossSeeAlso "GML", "XML" ;
            ns0:para "A meta-markup language, used to create markup languages such as DocBook."
          ] ;
          ns0:GlossSee "markup" ;
          ns0:GlossTerm "Standard Generalized Markup Language" ;
          ns0:ID "SGML" ;
          ns0:SortAs "SGML"
        ] ] ;
      ns0:title "S"
    ] ;
    ns0:title "example glossary"
  ] .

This is perhaps strange RDF, but it can be loaded and manipulated using RDF tools.

You can play with the example in the json-ld playground

like image 31
brinxmat Avatar answered Dec 22 '22 14:12

brinxmat