Transform JSON-LD in Java
In this tutorial, we’ll learn how to transform a Java Object to JSON-LD and vice versa. Also learn how to verify the schema of JSON-LD.
JSON-LD
JSON-LD is a JSON-based format which is used to represent structured data and linked data. Schema of JSON-LD can be found in documentation of schema.org.
JSON-LD is widely used in websites to markup the schema of website pages. This helps website indexers (such as Google) to understand the website better and boost SEO. Though the JSON-LD usage are not limited to websites, also used in REST Web services, and unstructured databases such as Apache CouchDB and MongoDB.
Transform Java Object to JSON-LD
Let’s transform the Java object to JSON-LD using jackson-jsonld which is a Jackson module that provides the annotation to generate JSON-LD documents.
Add Maven Dependency
If you are using maven, add jackson-jsonld dependency in your pom.xml
<dependency>
<groupId>com.io-informatics.oss</groupId>
<artifactId>jackson-jsonld</artifactId>
<version>0.1.1</version>
</dependency>
Transform Java Object to JSON-LD
Next, We create a Person
class and annotate it with json-ld annotations.
package com.cnc;
import ioinformarics.oss.jackson.module.jsonld.annotation.JsonldId;
import ioinformarics.oss.jackson.module.jsonld.annotation.JsonldProperty;
import ioinformarics.oss.jackson.module.jsonld.annotation.JsonldType;
@JsonldType("http://schema.org/Person")
public class Person {
@JsonldId
public String id;
@JsonldProperty("http://schema.org/name")
public String name;
@JsonldProperty("http://schema.org/jobTitle")
public String jobtitle;
@JsonldProperty("http://schema.org/url")
public String url;
}
Finally, we create a Person
object instance and transform it in JSON-LD
// Register Jsonld Module with Jackson
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
objectMapper.registerModule(new JsonldModule(() -> objectMapper.createObjectNode()));
// Create Person Instance
Person person = new Person();
person.id = "mailto:lahoti.ashish20@gmail.com";
person.name = "Ashish Lahoti";
person.jobtitle = "Software Developer";
person.url = "https://codingnconcepts.com";
// Transform to JSON-LD
String personJsonLd = objectMapper.writer().writeValueAsString(JsonldResource.Builder.create().build(person));
System.out.println(personJsonLd);
Output
{
"@context" : {
"jobtitle" : "http://schema.org/jobTitle",
"name" : "http://schema.org/name",
"url" : "http://schema.org/url"
},
"@type" : "http://schema.org/Person",
"name" : "Ashish Lahoti",
"jobtitle" : "Software Developer",
"url" : "https://codingnconcepts.com",
"@id" : "mailto:lahoti.ashish20@gmail.com"
}
Transform Nested Java Object to JSON-LD
We can also transform nested java objects to complex JSON-LD. Let’s annotate Organization
and Location
classes with json-ld annotations.
@JsonldType("http://schema.org/Organization")
public class Organization {
@JsonldId
public Integer id;
@JsonldProperty("http://schema.org/url")
public String url;
@JsonldProperty("http://schema.org/name")
public String name;
@JsonldProperty("http://schema.org/location")
public Location location;
}
@JsonldType("http://schema.org/PostalAddress")
public class Location {
@JsonldProperty("http://schema.org/streetAddress")
public String address;
@JsonldProperty("http://schema.org/addressLocality")
public String city;
@JsonldProperty("http://schema.org/addressRegion")
public String state;
@JsonldProperty("http://schema.org/addressCountry")
public String country;
@JsonldProperty("http://schema.org/postalCode")
public String zipcode;
}
Now, create an instance of Organization
which also have a Location
property and transform the instance to JSON-LD
// Register Jsonld Module with Jackson
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
objectMapper.registerModule(new JsonldModule(() -> objectMapper.createObjectNode()));
// Create Organization and Location Instances
Organization org = new Organization();
org.id = 12345;
org.url = "https://codingnconcepts.com";
org.name = "CodingNConcepts";
Location location = new Location();
location.address = "7 S. Broadway";
location.city = "Denver";
location.state = "CO";
location.country = "USA";
location.zipcode = "80209";
org.location = location;
// Transform to JSON-LD
String orgJsonLd = objectMapper.writer().writeValueAsString(JsonldResource.Builder.create().build(org));
System.out.println(orgJsonLd);
Output
{
"@context" : {
"name" : "http://schema.org/name",
"location" : "http://schema.org/location",
"url" : "http://schema.org/url"
},
"@type" : "http://schema.org/Organization",
"url" : "https://codingnconcepts.com",
"name" : "CodingNConcepts",
"location" : {
"@type" : "http://schema.org/PostalAddress",
"@context" : {
"zipcode" : "http://schema.org/postalCode",
"country" : "http://schema.org/addressCountry",
"address" : "http://schema.org/streetAddress",
"city" : "http://schema.org/addressLocality",
"state" : "http://schema.org/addressRegion"
},
"address" : "7 S. Broadway",
"city" : "Denver",
"state" : "CO",
"country" : "USA",
"zipcode" : "80209"
},
"@id" : 12345
}
Transform JSON-LD to Java Object
We can also transform the JSON-LD document to a plain JSON object and further transform the JSON to Java Object using Jackson Object Mapper. Let’s use jsonld-java which is a Java implementation of the JSON-LD 1.0 specification.
Add Maven Dependency
If you are using maven, add jsonld-java
dependency in your pom.xml
<dependency>
<groupId>com.github.jsonld-java</groupId>
<artifactId>jsonld-java</artifactId>
<version>0.13.0</version>
</dependency>
Transform JSON-LD to JSON
Let’s user the Person
JSON-LD and save it in a file input.json
input.json
{
"@context" : {
"jobtitle" : "http://schema.org/jobTitle",
"name" : "http://schema.org/name",
"url" : "http://schema.org/url"
},
"@type" : "http://schema.org/Person",
"name" : "Ashish Lahoti",
"jobtitle" : "Software Developer",
"url" : "https://codingnconcepts.com",
"@id" : "mailto:lahoti.ashish20@gmail.com"
}
Next, read from this file and transform to a simple JSON string
// Read from JSON-LD file
InputStream inputStream = new FileInputStream("input.json");
Object jsonObject = JsonUtils.fromInputStream(inputStream);
// Transform to Simple JSON compact object
Object compact = JsonLdProcessor.compact(jsonObject, new HashMap<>(), new JsonLdOptions());
// Convert JSON to String
String compactContent = JsonUtils.toPrettyString(compact);
System.out.println(compactContent);
Output
{
"@id" : "mailto:lahoti.ashish20@gmail.com",
"@type" : "http://schema.org/Person",
"http://schema.org/jobTitle" : "Software Developer",
"http://schema.org/name" : "Ashish Lahoti",
"http://schema.org/url" : "https://codingnconcepts.com"
}
Transform JSON to Java Object
We can further map this JSON string to a PersonJson
POJO using Jackson ObjectMapper
@JsonIgnoreProperties(ignoreUnknown = true)
public class PersonJson {
@JsonProperty("@id")
private String id;
@JsonProperty("@type")
private String type;
@JsonProperty("http://schema.org/name")
private String name;
@JsonProperty("http://schema.org/url")
private String url;
@JsonProperty("http://schema.org/jobTitle")
private String jobtitle;
}
// Map JSON to POJO
ObjectMapper objectMapper = new ObjectMapper();
PersonJson person = objectMapper.readValue(compactContent, PersonJson.class);
System.out.println(person);
Verify Transformed JSON-LD
You can verify the schema of transformed JSON-LD using Structured Data Testing Tool.
Click on the link and select “CODE SNIPPET” tab. Then copy paste the generated JSON-LD. Click “Run Test”. It tells if the schema of generated JSON-LD is correct and show errors if any.
The full source code of this tutorial is available on GitHub.