In this tutorial, we will show you how to use Jackson 2.x to convert Java object to/from JSON using Jackson API.
The class ObjectMapper is the main api of Jackson 2.x used for data-binding. It comes with several reader/writer methods to preform the conversion from/to Java and JSON.
Other interesting posts you may like
Quick Reference
Convert Java object to JSON using the writeValue() method
1 2 3 4 5 6 7 8 |
ObjectMapper mapper = new ObjectMapper(); Fruit fruit = new Fruit(); //Object to JSON in file mapper.writeValue(new File("C:/temp/fruit.json"), fruit); //Object to JSON in String String jsonInString = mapper.writeValueAsString(fruit ); |
Convert JSON to Java object using the readValue() method
1 2 3 4 5 6 7 8 9 10 11 |
ObjectMapper mapper = new ObjectMapper(); String jsonInString = "{'name':'Apple','cost':0,'produceby':['Indonesia','Combodia','Peru'],'fruitPackage':{'name':'Fruit Punnets','quantityNet':5}}"; //JSON from file to Object Staff obj = mapper.readValue(new File("C:/temp/fruit.json"), Fruit.class); //JSON from URL to Object Staff obj = mapper.readValue(new URL("http://.../fruit.json"), Fruit.class); //JSON from String to Object Staff obj = mapper.readValue(jsonInString, fruit.class); |
JACKSON dependency in pom.xml
We will use the below Jackson dependency form data-bind.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.javabycode.jackson</groupId> <artifactId>jackson-json</artifactId> <version>1.0.0-SNAPSHOT</version> <name>JSON-EXAMPLE - ${project.artifactId}</name> <packaging>jar</packaging> <dependencies> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.0</version> </dependency> </dependencies> </project> |
Create POJO (Plain Old Java Object)
We will use these Java class to Convert JSON to/from Java Objects.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
package com.javabycode.jackson; import java.util.ArrayList; import java.util.List; public class Fruit { private String name; private long cost; private List<String> produceby = new ArrayList<String>(); private FruitPackage fruitPackage = new FruitPackage(); public void setName(String name) { this.name = name; } public String getName() { return name; } public void setCost(long cost) { this.cost = cost; } public long getCost() { return cost; } public void setProduceby(List<String> produceby) { this.produceby = produceby; } public List<String> getProduceby() { return produceby; } public void setFruitPackage(FruitPackage fruitPackage) { this.fruitPackage = fruitPackage; } public FruitPackage getFruitPackage() { return fruitPackage; } public String toString(){ return "Fruit [name="+name+", cost="+cost+", produceby="+produceby+", fruitPackage="+fruitPackage+"]"; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package com.javabycode.jackson; public class FruitPackage { String name; Integer quantityNet; public void setName(String name) { this.name = name; } public String getName() { return name; } public void setQuantityNet(Integer quantityNet) { this.quantityNet = quantityNet; } public Integer getQuantityNet() { return quantityNet; } public String toString(){ return "FruitPackage [name="+name+", quantityNet="+quantityNet+"]"; } } |
Convert Java Object to JSON and write JSON to a file
Convert Fruit object into JSON and write that JSON to a file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
package com.javabycode.jackson; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import com.fasterxml.jackson.core.JsonGenerationException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; public class ConvertJavaObjectToJsonExample { public static void main(String args[]) { Fruit fruit = new Fruit(); fruit.setName("Apple"); List<String> nation = new ArrayList<String>(); nation.add("Indonesia"); nation.add("Combodia"); nation.add("Peru"); fruit.setProduceby(nation); FruitPackage fruitPackage1 = new FruitPackage(); fruitPackage1.setName("Fruit Punnets"); fruitPackage1.setQuantityNet(5); fruit.setFruitPackage(fruitPackage1); ObjectMapper mapper = new ObjectMapper(); try { /** * Write object to file */ mapper.writeValue(new File("C:/temp/fruit.json"), fruit);// Plain JSON mapper.writerWithDefaultPrettyPrinter().writeValue(new File("C:/temp/fruit-pretty.json"), fruit); // Prettified JSON String fruitString = mapper.writeValueAsString(fruit); System.out.println(fruitString); } catch (JsonGenerationException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } |
Run above program. The screen shot is the output written in file fruit.json
Pretty print JSON
The above output is not very readable. But you can print pretty JSON using writerWithDefaultPrettyPrinter factory method of ObjectMapper.
Read JSON from a file and convert into Java Object
Read JSON string from a file and convert into Fruit object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
package com.javabycode.jackson; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import com.fasterxml.jackson.core.JsonGenerationException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; public class ConvertJsonToJavaObjectExample { public static void main(String args[]) { ObjectMapper mapper = new ObjectMapper(); try { /** * Read object from file */ Fruit fruitFromFile = new Fruit(); fruitFromFile = mapper.readValue(new File("C:/temp/fruit.json"), Fruit.class); System.out.println(fruitFromFile); /** * Convert JSON string to Object */ String jsonString = "{\"name\":\"Apple\",\"cost\":0,\"produceby\":[\"Indonesia\",\"Combodia\",\"Peru\"],\"fruitPackage\":{\"name\":\"Fruit Punnets\",\"quantityNet\":5}}"; Fruit fruitFromString = mapper.readValue(jsonString, Fruit.class); System.out.println(fruitFromString); //Pretty print String prettyFruit = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(fruitFromString); System.out.println(prettyFruit); } catch (JsonGenerationException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } |
Following is the output:
That’s it. In the next post we will learn how Json can be mapped to Java Maps.
Download source code, click link below
Jackson-Json-Example.zip
Hello, how may I accomplish the same with BOTH a Model bean and a managed bean that connects the MODEL to a remote MySQL data source?
http://stackoverflow.com/questions/41142802/jsf-managed-bean-accessing-mysql-remote-database-how-to-create-json-array-to-f
Your link was broken