How to export the JSON-LD context when exporting RDF data?

70 views
Skip to first unread message

Maxime Lecoq

unread,
Jan 12, 2024, 4:14:39 AMJan 12
to vocbench-user
Hi,

In VocBench 10.2.1 I can't manage to get the JSON-LD context exported in the Global Data Management > Export Data window.

I'm using the "Save to file "deployer, the "RDF serializing exporter" reformatter and the "JSON-LD" export format.

I can't see any relevant option to activate in the configure window of the reformatter?

The JSON-LD data is successfully exported but it does not contain the context and URI are not prefixed.

How can we have the JSON-LD context also exported?

Thanks!

Thomas Francart

unread,
Jan 12, 2024, 4:46:04 AMJan 12
to Maxime Lecoq, vocbench-user
Hello Maxime

Does VocBench have such an option to export a JSON-LD context ? I doubt it since JSON-LD contexts drive the structure of the serialization you want, which is for you to define. VocBench (I think) just does a plain JSON-LD export, without any specific context information. My hypothesis (please anyone correct me if I am wrong) is that *you* need to manually write a context, or even a JSON-LD framing specification (which is basically a context + hierarchical structure information) and then frame the JSON-LD serialization provided by VocBench with this framing specification. See this blog post for more on JSON-LD 1.1 framing.

Cheers
Thomas


--
You received this message because you are subscribed to the Google Groups "vocbench-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vocbench-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vocbench-user/2fc58ef0-6060-4e64-97b7-22f6064ba014n%40googlegroups.com.


--

Thomas Francart - SPARNA
Web de données | Architecture de l'information | Accès aux connaissances
blog :
blog.sparna.fr, site : sparna.fr, linkedin : fr.linkedin.com/in/thomasfrancart
tel : 
 +33 (0)6.71.11.25.97
, skype : francartthomas

Maxime Lecoq

unread,
Jan 12, 2024, 5:38:26 AMJan 12
to vocbench-user
Hi Thomas!

Thanks for your response :) Nice blog post, thanks!

I guess I should use a VB reformatter to frame the JSON-LD serialization provided by VocBench with a framing specification?

Otherwise I was thinking that maybe VocBench could use the prefixes defined in the Metadata > Namespaces and imports window? So the context would be built automatically.

I looked in the source code of VB and saw that it relies on RDF4J when exporting to JSON-LD. In the documentation of RDF4J it seems we can provide some configuration options to process a context like the JSONLD_MODE (compact, expand, flatten), and the OPTIMIZE. Although these options seems to be deprecated and I don't see how to pass a context anyway to the Writer object. But maybe that's a lead.

Thomas Francart

unread,
Jan 12, 2024, 6:57:39 AMJan 12
to Maxime Lecoq, vocbench-user
Le ven. 12 janv. 2024 à 11:38, Maxime Lecoq <max...@lecoqlibre.fr> a écrit :
Hi Thomas!

Thanks for your response :) Nice blog post, thanks!

I guess I should use a VB reformatter to frame the JSON-LD serialization provided by VocBench with a framing specification?

Otherwise I was thinking that maybe VocBench could use the prefixes defined in the Metadata > Namespaces and imports window? So the context would be built automatically.

That could indeed be an option for namespaces. But you usually also want to map URI to prefix-less JSON keys. Of course a JSON-LD context generation algorithm could use the local part of classes and properties URI to derive the JSON key (e.g. http://xmlns.com/foaf/0.1/name would be mapped to "name"). But at the end, the exact JSON serialization you want depends on you.
 

I looked in the source code of VB and saw that it relies on RDF4J when exporting to JSON-LD. In the documentation of RDF4J it seems we can provide some configuration options to process a context like the JSONLD_MODE (compact, expand, flatten), and the OPTIMIZE.

The compaction algorithm of JSON-LD requires a JSON-LD context *as an input* (see https://www.w3.org/TR/json-ld11-api/#compaction), it will not compact a JSON-LD magically.

Thomas
 

Manuel Fiorelli

unread,
Jan 12, 2024, 6:43:32 PMJan 12
to Thomas Francart, Maxime Lecoq, vocbench-user
Hi Thomas and Maxime,

VocBench doesn't provide specific settings for JSON-LD or any other format, really. We just provide basic writer settings that are recognized (in a consistent way by many writers).

As an experiment, I wrote the following simple program using RDF4J (actually a newer version than the one in the old VocBench you mentioned).

import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.model.util.ModelBuilder;
import org.eclipse.rdf4j.model.vocabulary.OWL;
import org.eclipse.rdf4j.model.vocabulary.RDF;
import org.eclipse.rdf4j.model.vocabulary.RDFS;
import org.eclipse.rdf4j.rio.RDFFormat;
import org.eclipse.rdf4j.rio.Rio;
import org.eclipse.rdf4j.rio.WriterConfig;
import org.eclipse.rdf4j.rio.helpers.BasicWriterSettings;
import org.eclipse.rdf4j.rio.helpers.JSONLDMode;
import org.eclipse.rdf4j.rio.helpers.JSONLDSettings;

import java.net.URISyntaxException;

class Scratch {
public static void main(String[] args) throws URISyntaxException {
Model model = new ModelBuilder()
.setNamespace(OWL.NS)
.setNamespace(RDFS.NS)
.setNamespace("", "http://example.org/onto/")
.subject(":socrates")
.add(RDF.TYPE, ":Person")
.add(":knows", ":plato")
.subject(":Person")
.add(RDF.TYPE, OWL.CLASS)
.add(RDFS.SUBCLASSOF, ":Mortal")
.subject(":Mortal")
.add(RDF.TYPE, OWL.CLASS)
.build();

Rio.write(model, System.out, "", RDFFormat.JSONLD, new WriterConfig()
.set(JSONLDSettings.JSONLD_MODE, JSONLDMode.COMPACT)
.set(BasicWriterSettings.PRETTY_PRINT, true));
}
}

The code produced the following output:

{
  "@graph" : [ {
    "@id" : "http://example.org/onto/Mortal",
    "@type" : "owl:Class"
  }, {
    "@id" : "http://example.org/onto/Person",
    "@type" : "owl:Class",
    "rdfs:subClassOf" : {
      "@id" : "http://example.org/onto/Mortal"
    }
  }, {
    "@id" : "http://example.org/onto/socrates",
    "knows" : {
      "@id" : "http://example.org/onto/plato"
    },
    "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" : {
      "@id" : "http://example.org/onto/Person"
    }
  } ],
  "@context" : {
    "owl" : "http://www.w3.org/2002/07/owl#",
    "rdfs" : "http://www.w3.org/2000/01/rdf-schema#",
    "@vocab" : "http://example.org/onto/"
  }
}

Does that satisfy you? 
If not, consider asking for help on the RDF4J mailing list, where you are likely to find someone who can say the ultimate word on the JSON-LD generation capabilities.

Regards,
Manuel

Maxime Lecoq

unread,
Feb 5, 2024, 7:21:28 AMFeb 5
to vocbench-user
Thank you Manuel for the response and the code snippet which is very kind of you, I really appreciate it!

Unfortunately the team I belongs to decided that we are not going to invest more on this task for now.

But it's good to have your response maybe for later or for other people interested in the same feature!

Regards,
Maxime
Reply all
Reply to author
Forward
0 new messages