You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Grahame Grieve <gr...@healthintersections.com.au> on 2017/01/07 21:08:52 UTC

Overriding @context when loading JSON-LD

hi

I am trying to read a json-ld file into a Model so I can use IsoMatcher to
compare it with another Model.

When I try the simple approach:

   Model mj = RDFDataMgr.loadModel([json-ld filename]);

I get the error LOADING_REMOTE_CONTEXT_FAILED returned. I don't know why;
the file works in the json-ld playground. The source is actually
http://build.fhir.org/account-example.jsonld. Unfortunately, jsonld-java
suppresses the original exception.

But irrespective of that, I need to override the @context value and specify
my own local context instead (I'm actually running the build that generates
the content for build.fhir.org). I can't find out how to override the
context. At
http://stackoverflow.com/questions/41494678/how-can-i-override-the-context-when-loading-json-ld-in-jena
, it was suggested that I use read(Model model, String uri, Context context)
<https://jena.apache.org/documentation/javadoc/arq/org/apache/jena/riot/RDFDataMgr.html#read-org.apache.jena.rdf.model.Model-java.lang.String-org.apache.jena.sparql.util.Context->,
but I've drawn a blank trying to find information about how to use the
Context to do this.

Alternatively, I could use the loading method documented here:
https://github.com/jsonld-java/jsonld-java#code-example - but I have no
idea how to get to a Jena graph from there (and I haven't figured out how
make the custom resolution work in my Eclipse context either)

How can I get to a Jena graph using a context defined in code somewhere?
Grahame


-- 
-----
http://www.healthintersections.com.au / grahame@healthintersections.com.au
/ +61 411 867 065

Re: Overriding @context when loading JSON-LD

Posted by Grahame Grieve <gr...@healthintersections.com.au>.
A follow-up to this:

((Map) jsonLdObject).put("@context", ((Map) jsonLdContext).get("@context");

you have to grab the content of @context from the definitions to insert
into @context in the document.

Grahame



public class JsonLdReadTest {
>> @Test
>> public final void test() throws JsonGenerationException, IOException {
>>         // read the jsonld file (with a URI as value of @context)
>>         File f = new File("/Users/fps/Desktop/jsonldTest.json");
>>         Object jsonLdObject = JsonUtils.fromInputStream(new
>> FileInputStream(f));
>>
>>         // read the context file
>>         // (here from disk, normally read file at URI defined in @context)
>>         File ctxf = new File("/Users/fps/Desktop/jsonl
>> dTestContext.json");
>>         Object jsonLdContext = JsonUtils.fromInputStream(new
>> FileInputStream(ctxf));
>>
>>         // jsonldObject is a Map.
>>         // Replace value associated to @context
>>         ((Map) jsonLdObject).put("@context", jsonLdContext);
>>
>>         // the jsonld with the @context replaced
>>         String jsonld = JsonUtils.toString(jsonLdObject);
>>
>>         System.out.println(jsonld);
>>
>>         // no problem now to parse it
>>         Model m = parse(jsonld);
>> }
>>
>> private Model parse(String jsonld) {
>>   Model m = ModelFactory.createDefaultModel();
>>   StringReader reader = new StringReader(jsonld);
>>   m.read(reader, null, "JSON-LD");
>>   return m;
>> }
>>
>> }
>>
>>
>
>
> --
> -----
> http://www.healthintersections.com.au / grahame@healthintersections.com.au
> / +61 411 867 065 <+61%20411%20867%20065>
>



-- 
-----
http://www.healthintersections.com.au / grahame@healthintersections.com.au
/ +61 411 867 065

Re: Overriding @context when loading JSON-LD

Posted by Grahame Grieve <gr...@healthintersections.com.au>.
thanks very much. I had not thought of such a simple thing as just
replacing the context. Duh

Grahame


On Mon, Jan 9, 2017 at 4:24 AM, François-Paul Servant <
francoispaulservant@gmail.com> wrote:

> oops, there’s a typo in the code that I sent. Here is it again:
>
> import java.io.File;
> import java.io.FileInputStream;
> import java.io.IOException;
> import java.io.StringReader;
> import java.util.Map;
>
> import org.apache.jena.rdf.model.Model;
> import org.apache.jena.rdf.model.ModelFactory;
> import org.junit.Test;
>
> import com.fasterxml.jackson.core.JsonGenerationException;
> import com.github.jsonldjava.utils.JsonUtils;
>
> public class JsonLdReadTest {
> @Test
> public final void test() throws JsonGenerationException, IOException {
>         // read the jsonld file (with a URI as value of @context)
>         File f = new File("/Users/fps/Desktop/jsonldTest.json");
>         Object jsonLdObject = JsonUtils.fromInputStream(new
> FileInputStream(f));
>
>         // read the context file
>         // (here from disk, normally read file at URI defined in @context)
>         File ctxf = new File("/Users/fps/Desktop/jsonldTestContext.json");
>         Object jsonLdContext = JsonUtils.fromInputStream(new
> FileInputStream(ctxf));
>
>         // jsonldObject is a Map.
>         // Replace value associated to @context
>         ((Map) jsonLdObject).put("@context", jsonLdContext);
>
>         // the jsonld with the @context replaced
>         String jsonld = JsonUtils.toString(jsonLdObject);
>
>         System.out.println(jsonld);
>
>         // no problem now to parse it
>         Model m = parse(jsonld);
> }
>
> private Model parse(String jsonld) {
>   Model m = ModelFactory.createDefaultModel();
>   StringReader reader = new StringReader(jsonld);
>   m.read(reader, null, "JSON-LD");
>   return m;
> }
>
> }
>
>


-- 
-----
http://www.healthintersections.com.au / grahame@healthintersections.com.au
/ +61 411 867 065

Re: Overriding @context when loading JSON-LD

Posted by François-Paul Servant <fr...@gmail.com>.
oops, there’s a typo in the code that I sent. Here is it again:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.StringReader;
import java.util.Map;

import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.junit.Test;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.github.jsonldjava.utils.JsonUtils;

public class JsonLdReadTest {
@Test
public final void test() throws JsonGenerationException, IOException {
	// read the jsonld file (with a URI as value of @context)
	File f = new File("/Users/fps/Desktop/jsonldTest.json");
	Object jsonLdObject = JsonUtils.fromInputStream(new FileInputStream(f));
	
	// read the context file
	// (here from disk, normally read file at URI defined in @context)
	File ctxf = new File("/Users/fps/Desktop/jsonldTestContext.json");
	Object jsonLdContext = JsonUtils.fromInputStream(new FileInputStream(ctxf));
	
	// jsonldObject is a Map. 
	// Replace value associated to @context
	((Map) jsonLdObject).put("@context", jsonLdContext);
	
	// the jsonld with the @context replaced
	String jsonld = JsonUtils.toString(jsonLdObject);
	
	System.out.println(jsonld);
	
	// no problem now to parse it
	Model m = parse(jsonld);
}

private Model parse(String jsonld) {
  Model m = ModelFactory.createDefaultModel();
  StringReader reader = new StringReader(jsonld);
  m.read(reader, null, "JSON-LD");
  return m;
}

}


Re: Overriding @context when loading JSON-LD

Posted by François-Paul Servant <fr...@gmail.com>.
Hi,

here is a Test class using jsonld-java that should do what you want (override the @context). It reads the jsonld file (that includes an @context defined as a URI) and the file defining the @context from files on my disk. Just replace with your own files.

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.StringReader;
import java.util.Map;

import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.junit.Test;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.github.jsonldjava.utils.JsonUtils;

public class JsonLdReadTest {
@Test
public final void test() throws JsonGenerationException, IOException {
	// read the jsonld file (which contains a URI as value of @context)
	File f = new File("/Users/fps/Desktop/jsonldTest.json");
	Object jsonldObject = JsonUtils.fromInputStream(new FileInputStream(f));
	
	// read the context file
	// (here from disk, normally read file at URI defined in @context)
	File ctxf = new File("/Users/fps/Desktop/jsonldTestContext.json");
	Object jsonLdContext = JsonUtils.fromInputStream(new FileInputStream(ctxf));
	
	// jsonldObject is a Map. 
	// Replace value associated to @context
	((Map) jsonldObject).put("@context", jsonLdContext);
	
	// the jsonld with the @context replaced
	String jsonld = JsonUtils.toString(jsonLdContext);
	System.out.println(jsonld);
	
	// no problem now to parse it
	Model m = parse(jsonld);
}

private Model parse(String jsonld) {
  Model m = ModelFactory.createDefaultModel();
  StringReader reader = new StringReader(jsonld);
  m.read(reader, null, "JSON-LD");
  return m;
}
}

(Note that this is not optimal: the json is parsed twice)
HTH

fps

> Le 7 janv. 2017 à 22:08, Grahame Grieve <gr...@healthintersections.com.au> a écrit :
> 
> hi
> 
> I am trying to read a json-ld file into a Model so I can use IsoMatcher to
> compare it with another Model.
> 
> When I try the simple approach:
> 
>   Model mj = RDFDataMgr.loadModel([json-ld filename]);
> 
> I get the error LOADING_REMOTE_CONTEXT_FAILED returned. I don't know why;
> the file works in the json-ld playground. The source is actually
> http://build.fhir.org/account-example.jsonld. Unfortunately, jsonld-java
> suppresses the original exception.
> 
> But irrespective of that, I need to override the @context value and specify
> my own local context instead (I'm actually running the build that generates
> the content for build.fhir.org). I can't find out how to override the
> context. At
> http://stackoverflow.com/questions/41494678/how-can-i-override-the-context-when-loading-json-ld-in-jena
> , it was suggested that I use read(Model model, String uri, Context context)
> <https://jena.apache.org/documentation/javadoc/arq/org/apache/jena/riot/RDFDataMgr.html#read-org.apache.jena.rdf.model.Model-java.lang.String-org.apache.jena.sparql.util.Context->,
> but I've drawn a blank trying to find information about how to use the
> Context to do this.
> 
> Alternatively, I could use the loading method documented here:
> https://github.com/jsonld-java/jsonld-java#code-example - but I have no
> idea how to get to a Jena graph from there (and I haven't figured out how
> make the custom resolution work in my Eclipse context either)
> 
> How can I get to a Jena graph using a context defined in code somewhere?
> Grahame
> 
> 
> -- 
> -----
> http://www.healthintersections.com.au / grahame@healthintersections.com.au
> / +61 411 867 065