On 22/05/14 03:03, Hubert Le Van Gong wrote:
Greetings,
I have a set of N-Quad statements that relate to different graphs. How do I
go about exporting all those statements into a single JSON-LD object?
For instance, I would expect that something like:
<http://one.example/subject3> <http://one.example/predicate3> <
http://one.example/object3> <http://example.org/graph3> .
_:subject1 <http://an.example/predicate1> "object1" <
http://example.org/graph1> .
_:subject2 <http://an.example/predicate2> "object2" <
http://example.org/graph2> .
would end up as the following JSON-LD (disclaimer: I'm not 100% sure this
is correct, JSON-LD wise):
[
{
"@context": {
"predicate3": "http://one.example/predicate3"
},
"@id": "http://example.org/graph3",
"@graph":
[
{
"@id": "http://one.example/subject3",
"predicate3": "http://one.example/object3"
}
]
},
{
"@context": {
"predicate1": "http://an.example/predicate1"
},
"@id": "http://example.org/graph1",
"@graph":
[
{
"@id": "_:b0",
"predicate1": "object1"
}
]
},
{
"@context": {
"predicate2": "http://an.example/predicate2"
},
"@id": "http://example.org/graph2",
"@graph":
[
{
"@id": "_:b0",
"predicate2": "object2"
}
]
}
]
So far, the approach I took was to load the n-quads using:
RDFDataMgr.read(dataset, in, RDFLanguages.NQUADS);
and then iterate through the graph names, to get each model with:
Model model = dataset.getNamedModel(name);
Something like model.write(out, RDFLanguages.strLangJSONLD); works well to
convert the data to JSON-LD, however it does not add any graph info
(@graph).
While I could probably stitch those parts together, I was hoping for a
cleaner way to do this.
If you write the whole dataset:
RDFDataMgr.write(System.out, dataset, Lang.JSONLD) ;
all the graphs will come out, named.
If you want to print one graph, with it's name, create a separate
dataset, put just that graph in it, as a named graph, and print that.
Models don't have names - it's the container (dataset) that gives it
it's name. It can have several names (be included several times, or in
several places).
Andy
In particular, I tried to create a new model by uniting 2 models at a time
but it did not quite work (although I do see @graph added)...
Can anyone shed some light on the best approach for this?
Cheers,
Hubert
PS: my apologies if it's already been discussed before but I did not find
anything on this particular topic in the list archive.