The output is direct from jsonld-java [1], and isn't formatted by Jena.
You maybe better asking there.
As I understand it, the outer @graph is there to make the outermost JSON
item an object. Some standardations of JSON require the outer structure
to be an object, not an array.
Andy
[1] https://github.com/jsonld-java/jsonld-java
On 22/05/14 20:04, Hubert Le Van Gong wrote:
Hi Andy,
I get the exact same output (I'm running the latest code from the repo).
The way I understand the output is that it basically is a nameless graph
which *contains* the 3 named graphs described in the N-Quads, isn't it?
I agree, it is correct JSONLD-wise since the spec says you have to have one
default/nameless graph and zero to many named graphs (not sure why a
nameless default graph is required).
My question was whether we could have had the default nameless graph not as
a container to the named graphs? Maybe that does not make any sense, but I
was thinking maybe something like:
[
{ "@graph": {}},
{ "@graph": {},
"@id": "graph1"},
{ "@graph": {},
"@id": "graph2"},
{ "@graph": {},
"@id": "graph3"}
]
Thanks for your help,
Hubert
On Thu, May 22, 2014 at 11:19 AM, Andy Seaborne <[email protected]> wrote:
On 22/05/14 18:04, Hubert Le Van Gong wrote:
I'm using whatever default setting there is.
How do I check this?
It's not the default.
Exactly what are you seeing? I have run your example data (NQ) and get
the quads only once.
Code below: output:
{
"@graph" : [ {
"@graph" : [ {
"@id" : "_:b0",
"http://an.example/predicate1" : "object1"
} ],
"@id" : "http://example.org/graph1"
}, {
"@graph" : [ {
"@id" : "_:b1",
"http://an.example/predicate2" : "object2"
} ],
"@id" : "http://example.org/graph2"
}, {
"@graph" : [ {
"@id" : "http://one.example/subject3",
"http://one.example/predicate3" : {
"@id" : "http://one.example/object3"
}
} ],
"@id" : "http://example.org/graph3"
} ]
}
which isn't ideal pretty but is correct. "object1", "object2" and
"object3" occur only once.
"@graph" : [ {
"@graph" : [ {
"@id" : "_:b0",
"http://an.example/predicate1" : "object1"
} ],
"@id" : "http://example.org/graph1"
},
...
is a single named graph as written by jsonld-java.
The inner "@graph" and "@id" are members of the same JSON {}-object.
Andy
PS Which version of Jena are you using?
My example is from latest dev, jsonld-java v0.4
The current snapshot artifacts are now up-to-date.
package dev ;
import java.io.ByteArrayInputStream ;
import java.io.ByteArrayOutputStream ;
import org.apache.jena.atlas.logging.LogCtl ;
import org.apache.jena.riot.Lang ;
import org.apache.jena.riot.RDFDataMgr ;
import com.hp.hpl.jena.query.* ;
import com.hp.hpl.jena.tdb.TDBFactory ;
public class DevMain
{
static {
LogCtl.setLog4j() ;
}
public static void main(String... argv) throws Exception {
Dataset ds = TDBFactory.createDataset() ;
RDFDataMgr.read(ds, "D.nq") ;
ByteArrayOutputStream bout = new ByteArrayOutputStream() ;
RDFDataMgr.write(bout, ds, Lang.JSONLD) ;
bout.flush() ;
RDFDataMgr.write(System.out, ds, Lang.JSONLD) ;
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray())
;
Dataset ds1 = DatasetFactory.createMem() ;
RDFDataMgr.read(ds1, bin, null, Lang.JSONLD) ;
RDFDataMgr.write(System.out, ds1, Lang.TRIG) ;
System.exit(0) ;
}
Thanks,
Hubert
On Thu, May 22, 2014 at 9:50 AM, Andy Seaborne <[email protected]> wrote:
Do you have TDB defaultUnionGraph turned on?
On 22 May 2014 16:51, Hubert Le Van Gong <[email protected]> wrote:
Cool, it worked fine.
Just curious, I noticed that the named graphs end up embedded in the
required default (nameless) graph (see below).
Any particular reason to do this and not have the default graph empty,
followed by the named graphs?
Cheers,
Hubert
{
"@graph" :
[
{
"@graph" :
[...],
"@id" : "http://example.org/graph1"
},
{
"@graph" :
[...],
"@id" : "http://example.org/graph2"
},
]
}
On Thu, May 22, 2014 at 4:16 AM, Andy Seaborne <[email protected]> wrote:
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.