5

I'm aware that conversion of RDF to JSON-LD has some limitations, but I wonder if there is a good way of making the conversion avoid use of blank nodes?

For example, given an RDF graph:

@prefix ex: <http://example.org/ontology#> .
<http://example.org/x123> ex:house [
                                      a ex:House ;
                                      ex:houseNumber "1a" ;
                                      ex:doorColour "blue"
                                   ] ;
                          ex:house [
                                      a ex:House ;
                                      ex:houseNumber "1b" ;
                                      ex:doorColour "green"
                                   ] .

Is it possible, using (Java) JSON-LD to enforce conversion to an array-based representation of the bnodes:

{
  "id": "http://example.org/x123",
  "house": [{
    "type": "House",
    "houseNumber": "1a",
    "doorColour": "blue"
  }, {
    "type": "House",
    "houseNumber": "1b",
    "doorColour": "green"
  }],
  "@context": {
      "ex": "http://example.org/ontology#",
      "house": "ex:house",
      "houseNumber": "ex:houseNumber",
      "doorColour": "ex:doorColour",
      "House": "ex:House",
      "id": "@id",
      "type": "@type"
  }
}

Rather than something like:

{
  "@graph": [
    {
      "@id": "_:b0",
      "@type": "http://example.org/ontology#House",
      "http://example.org/ontology#doorColour": "blue",
      "http://example.org/ontology#houseNumber": "1a"
    },
    {
      "@id": "_:b1",
      "@type": "http://example.org/ontology#House",
      "http://example.org/ontology#doorColour": "green",
      "http://example.org/ontology#houseNumber": "1b"
    },
    {
      "@id": "http://example.org/x123",
      "http://example.org/ontology#house": [
        {
          "@id": "_:b0"
        },
        {
          "@id": "_:b1"
        }
      ]
    }
  ]
}

At the moment, I'm iterating over the statements in the graph and manually producing the JSON, but is it at all possible to do this using libraries like java-jsonld or some other JSON-LD technique?

Stanislav Kralin
  • 11,070
  • 4
  • 35
  • 58
brinxmat
  • 1,191
  • 1
  • 10
  • 13

1 Answers1

4

You can use framing to achieve that. Have a look at the library example in the JSON-LD playground. Unfortunately it is not standardized yet so various implementations may not produce exactly the same output and/or super different features

Markus Lanthaler
  • 3,683
  • 17
  • 20
  • Excellent! I got this to work in the way I wanted with the jsonld-java implementation. I wonder if it is also possible to supress @id and/or other elements with framing? – brinxmat Feb 26 '16 at 09:23
  • No, that's not possible at the moment but you can alias then to just id etc. if you prefer that – Markus Lanthaler Feb 26 '16 at 15:02
  • Find here a [java example using rdf4j and jsonldjava](http://stackoverflow.com/questions/43638342). – jschnasse Apr 26 '17 at 15:38