Re: Converting JSON to JSONLD

Dear Emri,

JSON-LD is not in itself a different format from JSON ; it is a way to 
interpret JSON documents as RDF graphs. JSON-LD distinguishes different 
kinds of JSON documents, in particular :

- documents in the *expanded form* are self-sufficient: they can be 
interpreted directly as an RDF graph. Documents in this form have a very 
strict structure, very verbose, and very unlike any "common" JSON file.

- documents in the *compact form* need a context to be interpreted as an 
RDF graph. Given the right context, any JSON document can be considered 
as a compact JSON-LD document (so "converting JSON to JSON-LD" is a 
strange way to frame the problem, IMO).

JSON-LD processor provide a number of algorithms for converting between 
RDF graphs, expanded JSON and compact JSON (see the figure here 
https://www.w3.org/Talks/2021/09-20-ddi-cdi/jsonld-algorithms.svg).

Actually, the algorithms specified by JSON-LD-API are more flexible than 
what the figure above shows. The input of the 'toRdf' and 'compact' 
algorithm are not required to be in the expanded form. If they are in a 
compact form, they first will be expanded, using the `@context` 
attribute in the data or the `expandContext` in the JsonLdOptions.

Now, considering your code below :

* the original document is a standard JSON document, so it is /not/ in 
the expanded form, so it is considered to be a compact form;

* when you try to compact it, it is therefore first expanded, but

     - it has no `@context` attribute, and
     - your JsonLdOptions has no `expandContext` either;

* so the expansion of your original data yields an empty graph, which is 
then compacted to an empty JSON object.

If you want to get RDF triples out of your JSON document, what you need 
to do is

* create a context for your JSON format

* put this context in the 'expandContext' or your JsonLdOptions

* use JSONLD.toRdf(jsonObject, callback, options)

   (see https://github.com/jsonld-java/jsonld-java#jsonldtriplecallback)


Hope this helps,

   pa

On 21/12/2021 18:05, emri mbiemri wrote:
>
> I am trying to convert JSON to RDF graphs. For this, I have to 
> initially convert JSON to JSONLD. I am trying to do this by using 
> JSONLD-JAVA <https://github.com/jsonld-java/jsonld-java>. The problem 
> is that I get only empty object as result. I know that I can add a 
> @context annotation manually for small and simple JSON models, which 
> works for very simple JSON files, but I have to work with quite big 
> JSON models like this 
> <https://github..com/iliriani/iliriangit/blob/master/CustomerForm.form%20(1).xml> which 
> can not be converted only by adding a context.
>
> Here is my java code:
>
> |String file = "C:\\Users\\Admin\\Desktop\\Forms\\test\\stack.json"; 
> // Open a valid json(-ld) input file InputStream inputStream = new 
> FileInputStream(file); Object jsonObject = 
> JsonUtils.fromInputStream(inputStream); // Create a context JSON map 
> containing prefixes and definitions Map context = new HashMap(); 
> System.out.println(jsonObject); // Create an instance of JsonLdOptions 
> with the standard JSON-LD options JsonLdOptions options = new 
> JsonLdOptions(); //Convert to JSONLD Object compact = 
> JsonLdProcessor.compact(jsonObject, context, options); // 
> System.out.println("Compact is: "+compact); // Object compact = 
> JsonLdProcessor.compact(jsonObject, new HashMap<>(), new 
> JsonLdOptions()); // Print out the result (or don't, it's your call!) 
> System.out.println(JsonUtils.toPrettyString(compact));} |
>
> -------------------------
>
> Hope for any help to convert JSON to JSONLD from your side. Thanks a lot.
>

Received on Wednesday, 22 December 2021 13:22:29 UTC