Over a million developers have joined DZone.

Mapping Complex JSON Structures With JDK8 Nashorn

Wondering how you can map a complex JSON structure to another JSON structure using Java? Read this awesome tutorial on mapping complex JSON structures.

· Java Zone

Discover how AppDynamics steps in to upgrade your performance game and prevent your enterprise from these top 10 Java performance problems, brought to you in partnership with AppDynamics.

How can you map a complex JSON structure to another JSON structure in Java? I think there are a few possible solutions in Java.

The first solution is to use a serialization framework like Jackson, GSON, or smart-json. The mapping is a piece of awkward Java code with a lot of if-else conditions. The result is hard to test and hard to maintain. Schematic it looks like this:

JSON -> Java objects -> Mapping -> Java objects -> JSON

An second approach is to use a templating framwork (like Freemarker or Velocity) in combination with a serialization framwork. The logic of the mapping has moved to the template. Schematic it looks like this:

JSON -> Java objects -> Apply template -> JSON

One of the issues with this approach is that the template must enforce that the result is a valid JSON structure. I have tried this approach and it is really hard to produce a valid JSON structure in all use cases.

You could also map your JSON to XML and create the mapping with an XSL transformations. The schematic looks like this:

JSON -> XML -> XSL transformation -> XML -> JSON

But the ideal schema looks like this:

JSON -> Mapping -> JSON

With JDK 8 and the Nashorn Javascript engine this becomes possible! This implementation provides JSON.parse() and JSON.stringify() by default.

Example Javascript:

function convert(val) {
  var json = JSON.stringify(val);
  var g = JSON.parse(json);
  var d = {
    chunkId: g.chunk.id,
    timestamp: g.chunk.timestamp
  return JSON.stringify(d);

Java code:

private ScriptEngineManager engineManager;
private ScriptEngine engine;
public MyConverter() {
  ClassPathResource resource = new ClassPathResource("/converter.js");
  InputStreamReader reader = new InputStreamReader(resource.getInputStream());
  engineManager = new ScriptEngineManager();
  engine = engineManager.getEngineByName("nashorn");
public String convert(String val){
  return (String) engine.eval("convert(" + source + ")");

I think this is -at the moment- the best approach. Java 9 will ship with native JSON support. Perhaps it will become easier in the future.

The Java Zone is brought to you in partnership with AppDynamics. AppDynamics helps you gain the fundamentals behind application performance, and implement best practices so you can proactively analyze and act on performance problems as they arise, and more specifically with your Java applications. Start a Free Trial.


Published at DZone with permission of Jethro Bakker. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}