While working with JSON we often need to parse the JSON into an object, or Map, or List, or to a custom object as well. To parse the JSON we have to use a dependency to process it easily.
But, starting with JDK 8u60+ the built-in Nashorn engine is capable to convert JSON content into java.util.Map
or java.util.List
. No external dependencies are required for parsing.
Here is an example with sample JSON. You can follow the below steps.
- Copy the below JSON into a file “test.json” and place it inside your java project.
{ "page": "1", "per_page": 10, "total": 23, "total_pages": 3, "data": [ { "competition": "UEFA Champions League", "year": 2014, "round": "GroupG", "team1": "Chelsea", "team2": "Schalke 04", "team1goals": "1", "team2goals": "1" }, { "competition": "UEFA Champions League", "year": 2014, "round": "GroupG", "team1": "Chelsea", "team2": "NK Maribor", "team1goals": "6", "team2goals": "0" }, { "competition": "UEFA Champions League", "year": 2014, "round": "GroupG", "team1": "Chelsea", "team2": "Sporting CP", "team1goals": "3", "team2goals": "1" }, { "competition": "UEFA Champions League", "year": 2014, "round": "R16", "team1": "Chelsea", "team2": "Paris Saint-Germain", "team1goals": "1", "team2goals": "1" }, { "competition": "English Premier League", "year": 2014, "round": "", "team1": "Chelsea", "team2": "Leicester City", "team1goals": "2", "team2goals": "0" }, { "competition": "English Premier League", "year": 2014, "round": "", "team1": "Chelsea", "team2": "Swansea City", "team1goals": "4", "team2goals": "2" }, { "competition": "English Premier League", "year": 2014, "round": "", "team1": "Chelsea", "team2": "Aston Villa", "team1goals": "3", "team2goals": "0" }, { "competition": "English Premier League", "year": 2014, "round": "", "team1": "Chelsea", "team2": "Arsenal", "team1goals": "2", "team2goals": "0" }, { "competition": "English Premier League", "year": 2014, "round": "", "team1": "Chelsea", "team2": "Queens Park Rangers", "team1goals": "2", "team2goals": "1" }, { "competition": "English Premier League", "year": 2014, "round": "", "team1": "Chelsea", "team2": "West Bromwich Albion", "team1goals": "2", "team2goals": "0" } ] }
2. Create a class “JSONParser.java” and put the below code inside it.
package com.codersdesks.json; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.util.List; import java.util.Map; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; import org.junit.Before; import org.junit.Test; public class JSONParser { private ScriptEngine scriptEngine; private String json; @Before public void setup() throws IOException { ScriptEngineManager sem = new ScriptEngineManager(); this.scriptEngine = sem.getEngineByName("javascript"); File file = new File("test.json"); this.json = new String(Files.readAllBytes(file.toPath())); } @Test public void parse() throws ScriptException { String script = "Java.asJSONCompatible(" + this.json + ")"; Object result = this.scriptEngine.eval(script); Map contents = (Map) result; contents.forEach((k,v) ->{ System.out.println("Key => "+k +" value =>"+contents.get(k)); }); List data = (List) contents.get("data"); data.forEach(d ->{ Map matchDetail = (Map) d; matchDetail.forEach((k,v) ->{ System.out.println("Key => "+k +" value =>"+matchDetail.get(k)); }); }); } }
Run the Junit test and you can see the below output.
Key => page value =>1 Key => per_page value =>10 Key => total value =>23 Key => total_pages value =>3 Key => data value =>[[object Object], [object Object], [object Object], [object Object], [object Object], [object Object], [object Object], [object Object], [object Object], [object Object]] Key => competition value =>UEFA Champions League Key => year value =>2014 Key => round value =>GroupG Key => team1 value =>Chelsea Key => team2 value =>Schalke 04 Key => team1goals value =>1 Key => team2goals value =>1 Key => competition value =>UEFA Champions League Key => year value =>2014 Key => round value =>GroupG Key => team1 value =>Chelsea Key => team2 value =>NK Maribor Key => team1goals value =>6 Key => team2goals value =>0 Key => competition value =>UEFA Champions League Key => year value =>2014 Key => round value =>GroupG Key => team1 value =>Chelsea Key => team2 value =>Sporting CP Key => team1goals value =>3 Key => team2goals value =>1 Key => competition value =>UEFA Champions League Key => year value =>2014 Key => round value =>R16 Key => team1 value =>Chelsea Key => team2 value =>Paris Saint-Germain Key => team1goals value =>1 Key => team2goals value =>1 Key => competition value =>English Premier League Key => year value =>2014 Key => round value => Key => team1 value =>Chelsea Key => team2 value =>Leicester City Key => team1goals value =>2 Key => team2goals value =>0 Key => competition value =>English Premier League Key => year value =>2014 Key => round value => Key => team1 value =>Chelsea Key => team2 value =>Swansea City Key => team1goals value =>4 Key => team2goals value =>2 Key => competition value =>English Premier League Key => year value =>2014 Key => round value => Key => team1 value =>Chelsea Key => team2 value =>Aston Villa Key => team1goals value =>3 Key => team2goals value =>0 Key => competition value =>English Premier League Key => year value =>2014 Key => round value => Key => team1 value =>Chelsea Key => team2 value =>Arsenal Key => team1goals value =>2 Key => team2goals value =>0 Key => competition value =>English Premier League Key => year value =>2014 Key => round value => Key => team1 value =>Chelsea Key => team2 value =>Queens Park Rangers Key => team1goals value =>2 Key => team2goals value =>1 Key => competition value =>English Premier League Key => year value =>2014 Key => round value => Key => team1 value =>Chelsea Key => team2 value =>West Bromwich Albion Key => team1goals value =>2 Key => team2goals value =>0
You can also read about JSON Schema and How to use Jackson databind with JSON
For more information, you can refer to the official Nashorn extensions documentation.
Happy Learning !!