5

EDIT: I changed my mind. I would find a way to generate the Java class and load the JSON as an object of that class.


I just discovered that exists a variant of JSON called JSON-LD.

It seems to me a more structured way of defining JSON, that reminds me XML with an associated schema, like XSD.

Can I create a Java class from JSON-LD, load it at runtime and use it to convert JSON-LD to an instantiation of that class?

I read the documentation of both the implementations but I found nothing about it. Maybe I read them bad?

8
  • 1
    The question is too broadly and poorly worded. Everything in Java is either a primitive or Object. So the quick answer: yes. JSON-LD is a fromat for representing RDF. RDF is representing in Java as a RDF Graph or RDF Model, that wraps a Graph. All these things - are instantions of json-ld, in your terms, and, obiviously, are java Object's. So, please clarify - why you can't work with RDF? Commented Jul 31, 2019 at 10:21
  • @ssz I think "with jsonld-java, or Apache Jena" makes it very specific... Commented Jul 31, 2019 at 10:23
  • @ssz: furthermore JSON-LD does not represent RDF. JSON-LD can be trasnformed to an RDF model. And RDF models are not Java objects at all, they must be converted. So ok, you're suggesting to convert JSON-LD to RDF and then RDF to a Java object. Is it correct? Is there not a library that directly converts from JSON-LD to a Java object? Commented Jul 31, 2019 at 13:08
  • "JSON-LD is a concrete RDF syntax": https://www.w3.org/TR/json-ld That means any json-ld document can be represent as a RDF Graph (maybe in generic sense). RDF - is not a format at all, to be convertible from some other format. But it is representation data in RDF form. There is a powerful API to work with it - Apache Jena, which uses com.github.jsonld-java:json-ld API as a parser. If you don't want to use any RDF API, just only parser, than, perhaps you have to write your own model-API. Commented Jul 31, 2019 at 13:24
  • 1
    Admit, that JSON-LD is a little bit more wide, than RDF, but usually it is used exactly for RDF serialization (dataset or graph). So, if there is no blank predicates, it can loaded as a Graph or Dataset, which are java Objects, if the used API is written on Java. By the way, a Graph may be backed by the triple store, so may not fully be present in memory. Commented Jul 31, 2019 at 13:50

2 Answers 2

1

Doing a Google search brought me to a library that will decode the JSON-LD into an "undefined" Object.

// Open a valid json(-ld) input file
InputStream inputStream = new FileInputStream("input.json");
// Read the file into an Object (The type of this object will be a List, Map, String, Boolean,
// Number or null depending on the root object in the file).
Object jsonObject = JsonUtils.fromInputStream(inputStream);
// Create a context JSON map containing prefixes and definitions
Map context = new HashMap();
// Customise context...
// Create an instance of JsonLdOptions with the standard JSON-LD options
JsonLdOptions options = new JsonLdOptions();
// Customise options...
// Call whichever JSONLD function you want! (e.g. compact)
Object compact = JsonLdProcessor.compact(jsonObject, context, options);
// Print out the result (or don't, it's your call!)
System.out.println(JsonUtils.toPrettyString(compact));

https://github.com/jsonld-java/jsonld-java

Apparently, it can take it from just a string as well, as if reading it from a file or some other source. How you access the contents of the object, I can't tell. The documentation seems to be moderately decent, though.

It seems to be an active project, as the last commit was only 4 days ago and has 30 contributors. The license is BSD 3-Clause, if that makes any difference to you.

I'm not in any way associate with this project. I'm not an author nor have I made any pull requests. It's just something I found.

Good luck and I hope this helped!

Sign up to request clarification or add additional context in comments.

2 Comments

And can I put also a specific class instead of Object jsonObject? Is there a way to create the class from the JSON-LD itself?
@MarcoSulla, I'm sure you could use Reflection to drop it into a class, and there might even be a way to do it automatically. I haven't used that library, so I don't know.
0

see this page: JSON-LD Module for Jackson

3 Comments

Well, but I have to create the class by hand :P
This chain may be possible: [JSON-LD file] --{JSON-LD for Jackson}-> [Java Object] --{Jackson or Gson}-> [JSON file] --{jsonschema2pojo}-> [Java Class]
I don't know how {JSON-LD for Jackson}-> [Java Object] is possible without the class...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.