{"id":1457,"date":"2022-09-10T01:58:23","date_gmt":"2023-08-06T20:33:05","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/uncategorized\/an-instructional-tutorial-on-the-jackson-json-java-parser-api\/"},"modified":"2024-03-16T15:46:36","modified_gmt":"2024-03-16T15:46:36","slug":"an-instructional-tutorial-on-the-jackson-json-java-parser-api","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/an-instructional-tutorial-on-the-jackson-json-java-parser-api\/","title":{"rendered":"An instructional tutorial on the Jackson JSON Java Parser API."},"content":{"rendered":"<p>Jackson JSON Java Parser is highly favored and also utilized in the Spring framework. However, the Java JSON Processing API is not user-friendly and lacks the capability to automatically convert JSON to Java objects and vice versa. Thankfully, there are alternative APIs available for JSON processing. In our previous article, we discovered the <a href=\"https:\/\/javadoc.io\/doc\/com.google.code.gson\/gson\/latest\/com.google.gson\/module-summary.html\">Google Gson API<\/a> and witnessed its user-friendly nature and ease of use.<\/p>\n<h2>The Java parser for Jackson JSON.<\/h2>\n<p>If we want to incorporate the Jackson JSON Java API into our project, one option is to include it in the project&#8217;s build path. Alternatively, if we are using Maven, we can include the following dependency.<\/p>\n<pre class=\"post-pre\"><code>&lt;dependency&gt;\r\n\t&lt;groupId&gt;com.fasterxml.jackson.core&lt;\/groupId&gt;\r\n\t&lt;artifactId&gt;jackson-databind&lt;\/artifactId&gt;\r\n\t&lt;version&gt;2.2.3&lt;\/version&gt;\r\n&lt;\/dependency&gt;\r\n<\/code><\/pre>\n<p>The jackson-databind jar relies on the jackson-core and jackson-annotations libraries. Therefore, if you are directly adding them to the build path, ensure you add all three to avoid encountering a runtime error. The Jackson JSON Parser API simplifies the conversion of JSON to POJO Objects and allows for easy conversion to Maps from JSON data. Additionally, Jackson also supports generics and directly converts them from JSON to an object.<\/p>\n<h2>Example of Jackson JSON<\/h2>\n<p>To illustrate the conversion of JSON to a POJO\/Java object, we will use a intricate example that includes nested objects and arrays. This conversion will involve the utilization of arrays, lists, and maps in Java objects. The JSON data that we will be working with is stored in a file called employee.txt, which follows the structure outlined below.<\/p>\n<pre class=\"post-pre\"><code>{\r\n  \"id\": 123,\r\n  \"name\": \"Pankaj\",\r\n  \"permanent\": true,\r\n  \"address\": {\r\n    \"street\": \"Albany Dr\",\r\n    \"city\": \"San Jose\",\r\n    \"zipcode\": 95129\r\n  },\r\n  \"phoneNumbers\": [\r\n    123456,\r\n    987654\r\n  ],\r\n  \"role\": \"Manager\",\r\n  \"cities\": [\r\n    \"Los Angeles\",\r\n    \"New York\"\r\n  ],\r\n  \"properties\": {\r\n    \"age\": \"29 years\",\r\n    \"salary\": \"1000 USD\"\r\n  }\r\n}\r\n<\/code><\/pre>\n<p>We possess java classes that correspond to the provided json data.<\/p>\n<pre class=\"post-pre\"><code>package com.scdev.jackson.model;\r\n\r\npublic class Address {\r\n\t\r\n\tprivate String street;\r\n\tprivate String city;\r\n\tprivate int zipcode;\r\n\t\r\n\tpublic String getStreet() {\r\n\t\treturn street;\r\n\t}\r\n\tpublic void setStreet(String street) {\r\n\t\tthis.street = street;\r\n\t}\r\n\tpublic String getCity() {\r\n\t\treturn city;\r\n\t}\r\n\tpublic void setCity(String city) {\r\n\t\tthis.city = city;\r\n\t}\r\n\tpublic int getZipcode() {\r\n\t\treturn zipcode;\r\n\t}\r\n\tpublic void setZipcode(int zipcode) {\r\n\t\tthis.zipcode = zipcode;\r\n\t}\r\n\t\r\n\t@Override\r\n\tpublic String toString(){\r\n\t\treturn getStreet() + \", \"+getCity()+\", \"+getZipcode();\r\n\t}\r\n}\r\n<\/code><\/pre>\n<p>The Address class corresponds to the inner object of the root JSON data.<\/p>\n<pre class=\"post-pre\"><code>package com.scdev.jackson.model;\r\n\r\nimport java.util.Arrays;\r\nimport java.util.List;\r\nimport java.util.Map;\r\n\r\npublic class Employee {\r\n\r\n\tprivate int id;\r\n\tprivate String name;\r\n\tprivate boolean permanent;\r\n\tprivate Address address;\r\n\tprivate long[] phoneNumbers;\r\n\tprivate String role;\r\n\tprivate List&lt;String&gt; cities;\r\n\tprivate Map&lt;String, String&gt; properties;\r\n\t\r\n\tpublic int getId() {\r\n\t\treturn id;\r\n\t}\r\n\tpublic void setId(int id) {\r\n\t\tthis.id = id;\r\n\t}\r\n\tpublic String getName() {\r\n\t\treturn name;\r\n\t}\r\n\tpublic void setName(String name) {\r\n\t\tthis.name = name;\r\n\t}\r\n\tpublic boolean isPermanent() {\r\n\t\treturn permanent;\r\n\t}\r\n\tpublic void setPermanent(boolean permanent) {\r\n\t\tthis.permanent = permanent;\r\n\t}\r\n\tpublic Address getAddress() {\r\n\t\treturn address;\r\n\t}\r\n\tpublic void setAddress(Address address) {\r\n\t\tthis.address = address;\r\n\t}\r\n\tpublic long[] getPhoneNumbers() {\r\n\t\treturn phoneNumbers;\r\n\t}\r\n\tpublic void setPhoneNumbers(long[] phoneNumbers) {\r\n\t\tthis.phoneNumbers = phoneNumbers;\r\n\t}\r\n\tpublic String getRole() {\r\n\t\treturn role;\r\n\t}\r\n\tpublic void setRole(String role) {\r\n\t\tthis.role = role;\r\n\t}\r\n\t\r\n\t@Override\r\n\tpublic String toString(){\r\n\t\tStringBuilder sb = new StringBuilder();\r\n\t\tsb.append(\"***** Employee Details *****\\n\");\r\n\t\tsb.append(\"ID=\"+getId()+\"\\n\");\r\n\t\tsb.append(\"Name=\"+getName()+\"\\n\");\r\n\t\tsb.append(\"Permanent=\"+isPermanent()+\"\\n\");\r\n\t\tsb.append(\"Role=\"+getRole()+\"\\n\");\r\n\t\tsb.append(\"Phone Numbers=\"+Arrays.toString(getPhoneNumbers())+\"\\n\");\r\n\t\tsb.append(\"Address=\"+getAddress()+\"\\n\");\r\n\t\tsb.append(\"Cities=\"+Arrays.toString(getCities().toArray())+\"\\n\");\r\n\t\tsb.append(\"Properties=\"+getProperties()+\"\\n\");\r\n\t\tsb.append(\"*****************************\");\r\n\t\t\r\n\t\treturn sb.toString();\r\n\t}\r\n\tpublic List&lt;String&gt; getCities() {\r\n\t\treturn cities;\r\n\t}\r\n\tpublic void setCities(List&lt;String&gt; cities) {\r\n\t\tthis.cities = cities;\r\n\t}\r\n\tpublic Map&lt;String, String&gt; getProperties() {\r\n\t\treturn properties;\r\n\t}\r\n\tpublic void setProperties(Map&lt;String, String&gt; properties) {\r\n\t\tthis.properties = properties;\r\n\t}\r\n}\r\n<\/code><\/pre>\n<p>The employee is the Java bean that represents the main JSON object. Now, we will explore how we can convert JSON to a Java object by utilizing the Jackson JSON parser API.<\/p>\n<pre class=\"post-pre\"><code>package com.scdev.jackson.json;\r\n\r\nimport java.io.File;\r\nimport java.io.IOException;\r\nimport java.io.StringWriter;\r\nimport java.nio.file.Files;\r\nimport java.nio.file.Paths;\r\nimport java.util.ArrayList;\r\nimport java.util.HashMap;\r\nimport java.util.Iterator;\r\nimport java.util.List;\r\nimport java.util.Map;\r\n\r\nimport com.fasterxml.jackson.core.type.TypeReference;\r\nimport com.fasterxml.jackson.databind.JsonNode;\r\nimport com.fasterxml.jackson.databind.ObjectMapper;\r\nimport com.fasterxml.jackson.databind.SerializationFeature;\r\nimport com.fasterxml.jackson.databind.node.ObjectNode;\r\nimport com.scdev.jackson.model.Address;\r\nimport com.scdev.jackson.model.Employee;\r\n\r\n\r\npublic class JacksonObjectMapperExample {\r\n\r\n\tpublic static void main(String[] args) throws IOException {\r\n\t\t\r\n\t\t\/\/read json file data to String\r\n\t\tbyte[] jsonData = Files.readAllBytes(Paths.get(\"employee.txt\"));\r\n\t\t\r\n\t\t\/\/create ObjectMapper instance\r\n\t\tObjectMapper objectMapper = new ObjectMapper();\r\n\t\t\r\n\t\t\/\/convert json string to object\r\n\t\tEmployee emp = objectMapper.readValue(jsonData, Employee.class);\r\n\t\t\r\n\t\tSystem.out.println(\"Employee Object\\n\"+emp);\r\n\t\t\r\n\t\t\/\/convert Object to json string\r\n\t\tEmployee emp1 = createEmployee();\r\n\t\t\/\/configure Object mapper for pretty print\r\n\t\tobjectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);\r\n\t\t\r\n\t\t\/\/writing to console, can write to any output stream such as file\r\n\t\tStringWriter stringEmp = new StringWriter();\r\n\t\tobjectMapper.writeValue(stringEmp, emp1);\r\n\t\tSystem.out.println(\"Employee JSON is\\n\"+stringEmp);\r\n\t}\r\n\t\r\n\tpublic static Employee createEmployee() {\r\n\r\n\t\tEmployee emp = new Employee();\r\n\t\temp.setId(100);\r\n\t\temp.setName(\"David\");\r\n\t\temp.setPermanent(false);\r\n\t\temp.setPhoneNumbers(new long[] { 123456, 987654 });\r\n\t\temp.setRole(\"Manager\");\r\n\r\n\t\tAddress add = new Address();\r\n\t\tadd.setCity(\"Bangalore\");\r\n\t\tadd.setStreet(\"BTM 1st Stage\");\r\n\t\tadd.setZipcode(560100);\r\n\t\temp.setAddress(add);\r\n\r\n\t\tList&lt;String&gt; cities = new ArrayList&lt;String&gt;();\r\n\t\tcities.add(\"Los Angeles\");\r\n\t\tcities.add(\"New York\");\r\n\t\temp.setCities(cities);\r\n\r\n\t\tMap&lt;String, String&gt; props = new HashMap&lt;String, String&gt;();\r\n\t\tprops.put(\"salary\", \"1000 Rs\");\r\n\t\tprops.put(\"age\", \"28 years\");\r\n\t\temp.setProperties(props);\r\n\r\n\t\treturn emp;\r\n\t}\r\n\r\n}\r\n<\/code><\/pre>\n<p>When the program above is executed, the resulting output will be displayed.<\/p>\n<pre class=\"post-pre\"><code>Employee Object\r\n***** Employee Details *****\r\nID=123\r\nName=Pankaj\r\nPermanent=true\r\nRole=Manager\r\nPhone Numbers=[123456, 987654]\r\nAddress=Albany Dr, San Jose, 95129\r\nCities=[Los Angeles, New York]\r\nProperties={age=29 years, salary=1000 USD}\r\n*****************************\r\nEmployee JSON is\r\n\/\/printing same as above json file data\r\n<\/code><\/pre>\n<p>The com.fasterxml.jackson.databind.ObjectMapper is the key class in the Jackson API. It offers readValue() and writeValue() methods for converting JSON to a Java Object and vice versa. The ObjectMapper class can be reused and initialized as a Singleton object. It offers numerous versions of readValue() and writeValue() methods for working with byte arrays, Files, input\/output streams, and Reader\/Writer objects.<\/p>\n<h3>Converting JSON to Map using Jackson JSON.<\/h3>\n<p>Occasionally, we might come across a JSON object similar to the one provided, within the data.txt file.<\/p>\n<pre class=\"post-pre\"><code>{\r\n  \"name\": \"David\",\r\n  \"role\": \"Manager\",\r\n  \"city\": \"Los Angeles\"\r\n}\r\n<\/code><\/pre>\n<p>We aim to transform it into a Map instead of a Java object that has the same properties and keys. This can be easily done in Jackson JSON API using the following code which utilizes two methods.<\/p>\n<pre class=\"post-pre\"><code>\/\/converting json to Map\r\nbyte[] mapData = Files.readAllBytes(Paths.get(\"data.txt\"));\r\nMap&lt;String,String&gt; myMap = new HashMap&lt;String, String&gt;();\r\n\r\nObjectMapper objectMapper = new ObjectMapper();\r\nmyMap = objectMapper.readValue(mapData, HashMap.class);\r\nSystem.out.println(\"Map is: \"+myMap);\r\n\r\n\/\/another way\r\nmyMap = objectMapper.readValue(mapData, new TypeReference&lt;HashMap&lt;String,String&gt;&gt;() {});\r\nSystem.out.println(\"Map using TypeReference: \"+myMap);\r\n<\/code><\/pre>\n<p>After we run the above code, we obtain the following result.<\/p>\n<pre class=\"post-pre\"><code>Map is: {name=David, role=Manager, city=Los Angeles}\r\nMap using TypeReference: {name=David, role=Manager, city=Los Angeles}\r\n<\/code><\/pre>\n<h3>Read a specific JSON key using Jackson JSON.<\/h3>\n<p>There are instances where we only require certain values from a json data. In such cases, it is not advisable to convert the entire JSON into an object. The Jackson JSON API offers an alternative by allowing us to read json data in a tree-like DOM Parser manner. This enables us to access specific elements within the JSON object. The code provided below demonstrates how to extract specific entries from a json file.<\/p>\n<pre class=\"post-pre\"><code>\/\/read json file data to String\r\nbyte[] jsonData = Files.readAllBytes(Paths.get(\"employee.txt\"));\r\n\r\n\/\/create ObjectMapper instance\r\nObjectMapper objectMapper = new ObjectMapper();\r\n\r\n\/\/read JSON like DOM Parser\r\nJsonNode rootNode = objectMapper.readTree(jsonData);\r\nJsonNode idNode = rootNode.path(\"id\");\r\nSystem.out.println(\"id = \"+idNode.asInt());\r\n\r\nJsonNode phoneNosNode = rootNode.path(\"phoneNumbers\");\r\nIterator&lt;JsonNode&gt; elements = phoneNosNode.elements();\r\nwhile(elements.hasNext()){\r\n\tJsonNode phone = elements.next();\r\n\tSystem.out.println(\"Phone No = \"+phone.asLong());\r\n}\r\n<\/code><\/pre>\n<p>When we run the code snippet, we receive the subsequent result.<\/p>\n<pre class=\"post-pre\"><code>id = 123\r\nPhone No = 123456\r\nPhone No = 987654\r\n<\/code><\/pre>\n<h3>Jackson JSON offers the capability to modify a JSON document.<\/h3>\n<p>The Java API of Jackson JSON offers convenient options for adding, modifying, and deleting keys in JSON data. Afterward, we can either save it as a new JSON file or write it to any stream. The following code illustrates how to accomplish this effortlessly.<\/p>\n<pre class=\"post-pre\"><code>byte[] jsonData = Files.readAllBytes(Paths.get(\"employee.txt\"));\r\n\r\nObjectMapper objectMapper = new ObjectMapper();\r\n\r\n\/\/create JsonNode\r\nJsonNode rootNode = objectMapper.readTree(jsonData);\r\n\r\n\/\/update JSON data\r\n((ObjectNode) rootNode).put(\"id\", 500);\r\n\/\/add new key value\r\n((ObjectNode) rootNode).put(\"test\", \"test value\");\r\n\/\/remove existing key\r\n((ObjectNode) rootNode).remove(\"role\");\r\n((ObjectNode) rootNode).remove(\"properties\");\r\nobjectMapper.writeValue(new File(\"updated_emp.txt\"), rootNode);\r\n<\/code><\/pre>\n<p>Once you run the above code and check the new file, you&#8217;ll observe that the &#8220;role&#8221; and &#8220;properties&#8221; keys are missing. Additionally, you&#8217;ll see that the &#8220;id&#8221; value is now 500 and a new key called &#8220;test&#8221; has been included in the updated_emp.txt file.<\/p>\n<h3>One possible paraphrase of &#8220;Jackson JSON Streaming API Example&#8221; could be &#8220;An instance showcasing the usage of the Jackson JSON Streaming API.&#8221;<\/h3>\n<p>The Jackson JSON Java API also offers streaming capabilities, which are beneficial when dealing with large JSON data. This feature allows the API to read the file as tokens, resulting in reduced memory usage. The only drawback of using the streaming API is that we need to handle all the tokens during the parsing of the JSON data. For instance, if we have the JSON data {&#8220;role&#8221;:&#8221;Manager&#8221;}, the tokens would be retrieved in the following order: { (start object), &#8220;role&#8221; (key name), &#8220;Manager&#8221; (key value), and } (end object). It is important to note that the colon (:) is not considered a token since it serves as a delimiter in JSON.<\/p>\n<pre class=\"post-pre\"><code>package com.scdev.jackson.json;\r\n\r\nimport java.io.File;\r\nimport java.io.IOException;\r\nimport java.util.ArrayList;\r\nimport java.util.HashMap;\r\nimport java.util.List;\r\n\r\nimport com.fasterxml.jackson.core.JsonFactory;\r\nimport com.fasterxml.jackson.core.JsonParseException;\r\nimport com.fasterxml.jackson.core.JsonParser;\r\nimport com.fasterxml.jackson.core.JsonToken;\r\nimport com.scdev.jackson.model.Address;\r\nimport com.scdev.jackson.model.Employee;\r\n\r\npublic class JacksonStreamingReadExample {\r\n\r\n\tpublic static void main(String[] args) throws JsonParseException, IOException {\r\n\t\t\r\n\t\t\/\/create JsonParser object\r\n\t\tJsonParser jsonParser = new JsonFactory().createParser(new File(\"employee.txt\"));\r\n\t\t\r\n\t\t\/\/loop through the tokens\r\n\t\tEmployee emp = new Employee();\r\n\t\tAddress address = new Address();\r\n\t\temp.setAddress(address);\r\n\t\temp.setCities(new ArrayList&lt;String&gt;());\r\n\t\temp.setProperties(new HashMap&lt;String, String&gt;());\r\n\t\tList&lt;Long&gt; phoneNums = new ArrayList&lt;Long&gt;();\r\n\t\tboolean insidePropertiesObj=false;\r\n\t\t\r\n\t\tparseJSON(jsonParser, emp, phoneNums, insidePropertiesObj);\r\n\t\t\r\n\t\tlong[] nums = new long[phoneNums.size()];\r\n\t\tint index = 0;\r\n\t\tfor(Long l :phoneNums){\r\n\t\t\tnums[index++] = l;\r\n\t\t}\r\n\t\temp.setPhoneNumbers(nums);\r\n\t\t\r\n\t\tjsonParser.close();\r\n\t\t\/\/print employee object\r\n\t\tSystem.out.println(\"Employee Object\\n\\n\"+emp);\r\n\t}\r\n\r\n\tprivate static void parseJSON(JsonParser jsonParser, Employee emp,\r\n\t\t\tList&lt;Long&gt; phoneNums, boolean insidePropertiesObj) throws JsonParseException, IOException {\r\n\t\t\r\n\t\t\/\/loop through the JsonTokens\r\n\t\twhile(jsonParser.nextToken() != JsonToken.END_OBJECT){\r\n\t\t\tString name = jsonParser.getCurrentName();\r\n\t\t\tif(\"id\".equals(name)){\r\n\t\t\t\tjsonParser.nextToken();\r\n\t\t\t\temp.setId(jsonParser.getIntValue());\r\n\t\t\t}else if(\"name\".equals(name)){\r\n\t\t\t\tjsonParser.nextToken();\r\n\t\t\t\temp.setName(jsonParser.getText());\r\n\t\t\t}else if(\"permanent\".equals(name)){\r\n\t\t\t\tjsonParser.nextToken();\r\n\t\t\t\temp.setPermanent(jsonParser.getBooleanValue());\r\n\t\t\t}else if(\"address\".equals(name)){\r\n\t\t\t\tjsonParser.nextToken();\r\n\t\t\t\t\/\/nested object, recursive call\r\n\t\t\t\tparseJSON(jsonParser, emp, phoneNums, insidePropertiesObj);\r\n\t\t\t}else if(\"street\".equals(name)){\r\n\t\t\t\tjsonParser.nextToken();\r\n\t\t\t\temp.getAddress().setStreet(jsonParser.getText());\r\n\t\t\t}else if(\"city\".equals(name)){\r\n\t\t\t\tjsonParser.nextToken();\r\n\t\t\t\temp.getAddress().setCity(jsonParser.getText());\r\n\t\t\t}else if(\"zipcode\".equals(name)){\r\n\t\t\t\tjsonParser.nextToken();\r\n\t\t\t\temp.getAddress().setZipcode(jsonParser.getIntValue());\r\n\t\t\t}else if(\"phoneNumbers\".equals(name)){\r\n\t\t\t\tjsonParser.nextToken();\r\n\t\t\t\twhile (jsonParser.nextToken() != JsonToken.END_ARRAY) {\r\n\t\t\t\t\tphoneNums.add(jsonParser.getLongValue());\r\n\t\t\t\t}\r\n\t\t\t}else if(\"role\".equals(name)){\r\n\t\t\t\tjsonParser.nextToken();\r\n\t\t\t\temp.setRole(jsonParser.getText());\r\n\t\t\t}else if(\"cities\".equals(name)){\r\n\t\t\t\tjsonParser.nextToken();\r\n\t\t\t\twhile (jsonParser.nextToken() != JsonToken.END_ARRAY) {\r\n\t\t\t\t\temp.getCities().add(jsonParser.getText());\r\n\t\t\t\t}\r\n\t\t\t}else if(\"properties\".equals(name)){\r\n\t\t\t\tjsonParser.nextToken();\r\n\t\t\t\twhile(jsonParser.nextToken() != JsonToken.END_OBJECT){\r\n\t\t\t\t\tString key = jsonParser.getCurrentName();\r\n\t\t\t\t\tjsonParser.nextToken();\r\n\t\t\t\t\tString value = jsonParser.getText();\r\n\t\t\t\t\temp.getProperties().put(key, value);\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n}\r\n<\/code><\/pre>\n<p>JsonParser is the streaming API provided by Jackson to read and process JSON data. We utilize it for reading data from a file and then employ the parseJSON() function to iterate through the tokens and construct our Java object. It is important to observe that the parseJSON() function is recursively invoked for &#8220;address&#8221; as it is a nested object within the JSON data. When parsing arrays, we iterate through the JSON document. To generate JSON data using the streaming API, we can make use of the JsonGenerator class.<\/p>\n<pre class=\"post-pre\"><code>package com.scdev.jackson.json;\r\n\r\nimport java.io.FileOutputStream;\r\nimport java.io.IOException;\r\nimport java.util.Set;\r\n\r\nimport com.fasterxml.jackson.core.JsonFactory;\r\nimport com.fasterxml.jackson.core.JsonGenerator;\r\nimport com.fasterxml.jackson.core.util.DefaultPrettyPrinter;\r\nimport com.scdev.jackson.model.Employee;\r\n\r\npublic class JacksonStreamingWriteExample {\r\n\r\n\tpublic static void main(String[] args) throws IOException {\r\n\t\tEmployee emp = JacksonObjectMapperExample.createEmployee();\r\n\r\n\t\tJsonGenerator jsonGenerator = new JsonFactory()\r\n\t\t\t\t.createGenerator(new FileOutputStream(\"stream_emp.txt\"));\r\n\t\t\/\/for pretty printing\r\n\t\tjsonGenerator.setPrettyPrinter(new DefaultPrettyPrinter());\r\n\t\t\r\n\t\tjsonGenerator.writeStartObject(); \/\/ start root object\r\n\t\tjsonGenerator.writeNumberField(\"id\", emp.getId());\r\n\t\tjsonGenerator.writeStringField(\"name\", emp.getName());\r\n\t\tjsonGenerator.writeBooleanField(\"permanent\", emp.isPermanent());\r\n\t\t\r\n\t\tjsonGenerator.writeObjectFieldStart(\"address\"); \/\/start address object\r\n\t\t\tjsonGenerator.writeStringField(\"street\", emp.getAddress().getStreet());\r\n\t\t\tjsonGenerator.writeStringField(\"city\", emp.getAddress().getCity());\r\n\t\t\tjsonGenerator.writeNumberField(\"zipcode\", emp.getAddress().getZipcode());\r\n\t\tjsonGenerator.writeEndObject(); \/\/end address object\r\n\t\t\r\n\t\tjsonGenerator.writeArrayFieldStart(\"phoneNumbers\");\r\n\t\t\tfor(long num : emp.getPhoneNumbers())\r\n\t\t\t\tjsonGenerator.writeNumber(num);\r\n\t\tjsonGenerator.writeEndArray();\r\n\t\t\r\n\t\tjsonGenerator.writeStringField(\"role\", emp.getRole());\r\n\t\t\r\n\t\tjsonGenerator.writeArrayFieldStart(\"cities\"); \/\/start cities array\r\n\t\tfor(String city : emp.getCities())\r\n\t\t\tjsonGenerator.writeString(city);\r\n\t\tjsonGenerator.writeEndArray(); \/\/closing cities array\r\n\t\t\r\n\t\tjsonGenerator.writeObjectFieldStart(\"properties\");\r\n\t\t\tSet&lt;String&gt; keySet = emp.getProperties().keySet();\r\n\t\t\tfor(String key : keySet){\r\n\t\t\t\tString value = emp.getProperties().get(key);\r\n\t\t\t\tjsonGenerator.writeStringField(key, value);\r\n\t\t\t}\r\n\t\tjsonGenerator.writeEndObject(); \/\/closing properties\r\n\t\tjsonGenerator.writeEndObject(); \/\/closing root object\r\n\t\t\r\n\t\tjsonGenerator.flush();\r\n\t\tjsonGenerator.close();\r\n\t}\r\n\r\n}\r\n<\/code><\/pre>\n<p>Comparatively, JsonGenerator is simpler to use than JsonParser. This serves as a quick reference tutorial for the Jackson JSON Parser Java API. The Jackson JSON Java API is user-friendly and offers numerous options to facilitate developers when working with JSON data. You can download the project from the provided link and experiment with it to discover more options available in the Jackson Json API.<\/p>\n<p>Get the Jackson JSON Project.<\/p>\n<p>Source: Jackson&#8217;s GitHub Page<\/p>\n<p>Rephrased: Page on GitHub belonging to Jackson<\/p>\n<p>&nbsp;<\/p>\n<p>More tutorials<\/p>\n<p><a class=\"LinkSuggestion__Link-sc-1gewdgc-4 cLBplk\" href=\"https:\/\/www.silicloud.com\/blog\/a-brief-overview-of-jsons-fundamentals\/\" target=\"_blank\" rel=\"noopener\">JSON fundamentals<span class=\"sc-gswNZR eASTkv\">(Opens in a new browser tab)<\/span><\/a><\/p>\n<p><a class=\"LinkSuggestion__Link-sc-1gewdgc-4 cLBplk\" href=\"https:\/\/www.silicloud.com\/blog\/spring-rest-xml-and-json\/\" target=\"_blank\" rel=\"noopener\">One example of Spring REST XML and JSON<span class=\"sc-gswNZR eASTkv\">(Opens in a new browser tab)<\/span><\/a><\/p>\n<p><a class=\"LinkSuggestion__Link-sc-1gewdgc-4 cLBplk\" href=\"https:\/\/www.silicloud.com\/blog\/how-can-json-data-be-modified-using-jq\/\" target=\"_blank\" rel=\"noopener\">How can JSON Data be modified using jq?<span class=\"sc-gswNZR eASTkv\">(Opens in a new browser tab)<\/span><\/a><\/p>\n<p><a class=\"LinkSuggestion__Link-sc-1gewdgc-4 cLBplk\" href=\"https:\/\/www.silicloud.com\/blog\/okhttp-on-android\/\" target=\"_blank\" rel=\"noopener\">OkHttp on Android<span class=\"sc-gswNZR eASTkv\">(Opens in a new browser tab)<\/span><\/a><\/p>\n<p><a class=\"LinkSuggestion__Link-sc-1gewdgc-4 cLBplk\" href=\"https:\/\/www.silicloud.com\/blog\/the-objectoutputstream-in-java\/\" target=\"_blank\" rel=\"noopener\">the ObjectOutputStream in Java<span class=\"sc-gswNZR eASTkv\">(Opens in a new browser tab)<\/span><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Jackson JSON Java Parser is highly favored and also utilized in the Spring framework. However, the Java JSON Processing API is not user-friendly and lacks the capability to automatically convert JSON to Java objects and vice versa. Thankfully, there are alternative APIs available for JSON processing. In our previous article, we discovered the Google Gson [&hellip;]<\/p>\n","protected":false},"author":14,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-1457","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>An instructional tutorial on the Jackson JSON Java Parser API. - Blog - Silicon Cloud<\/title>\n<meta name=\"description\" content=\"Jackson JSON Java Parser is highly favored and also utilized in the Spring framework. However, the Java JSON Processing API is not\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/blog\/an-instructional-tutorial-on-the-jackson-json-java-parser-api\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"An instructional tutorial on the Jackson JSON Java Parser API.\" \/>\n<meta property=\"og:description\" content=\"Jackson JSON Java Parser is highly favored and also utilized in the Spring framework. However, the Java JSON Processing API is not\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/an-instructional-tutorial-on-the-jackson-json-java-parser-api\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-08-06T20:33:05+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-16T15:46:36+00:00\" \/>\n<meta name=\"author\" content=\"Noah Thompson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Noah Thompson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/an-instructional-tutorial-on-the-jackson-json-java-parser-api\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/an-instructional-tutorial-on-the-jackson-json-java-parser-api\/\"},\"author\":{\"name\":\"Noah Thompson\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/2e83cc6ab9f60d36921c2d0f9f280f4a\"},\"headline\":\"An instructional tutorial on the Jackson JSON Java Parser API.\",\"datePublished\":\"2023-08-06T20:33:05+00:00\",\"dateModified\":\"2024-03-16T15:46:36+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/an-instructional-tutorial-on-the-jackson-json-java-parser-api\/\"},\"wordCount\":1009,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/an-instructional-tutorial-on-the-jackson-json-java-parser-api\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/an-instructional-tutorial-on-the-jackson-json-java-parser-api\/\",\"name\":\"An instructional tutorial on the Jackson JSON Java Parser API. - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2023-08-06T20:33:05+00:00\",\"dateModified\":\"2024-03-16T15:46:36+00:00\",\"description\":\"Jackson JSON Java Parser is highly favored and also utilized in the Spring framework. However, the Java JSON Processing API is not\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/an-instructional-tutorial-on-the-jackson-json-java-parser-api\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/an-instructional-tutorial-on-the-jackson-json-java-parser-api\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/an-instructional-tutorial-on-the-jackson-json-java-parser-api\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"An instructional tutorial on the Jackson JSON Java Parser API.\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/2e83cc6ab9f60d36921c2d0f9f280f4a\",\"name\":\"Noah Thompson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/350e537e1530ede2762ee0237e877d6693f4f7163ab4f303202cc9a6b27b6cb4?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/350e537e1530ede2762ee0237e877d6693f4f7163ab4f303202cc9a6b27b6cb4?s=96&d=mm&r=g\",\"caption\":\"Noah Thompson\"},\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/noahthompson\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"An instructional tutorial on the Jackson JSON Java Parser API. - Blog - Silicon Cloud","description":"Jackson JSON Java Parser is highly favored and also utilized in the Spring framework. However, the Java JSON Processing API is not","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/blog\/an-instructional-tutorial-on-the-jackson-json-java-parser-api\/","og_locale":"en_US","og_type":"article","og_title":"An instructional tutorial on the Jackson JSON Java Parser API.","og_description":"Jackson JSON Java Parser is highly favored and also utilized in the Spring framework. However, the Java JSON Processing API is not","og_url":"https:\/\/www.silicloud.com\/blog\/an-instructional-tutorial-on-the-jackson-json-java-parser-api\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2023-08-06T20:33:05+00:00","article_modified_time":"2024-03-16T15:46:36+00:00","author":"Noah Thompson","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"Noah Thompson","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/an-instructional-tutorial-on-the-jackson-json-java-parser-api\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/an-instructional-tutorial-on-the-jackson-json-java-parser-api\/"},"author":{"name":"Noah Thompson","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/2e83cc6ab9f60d36921c2d0f9f280f4a"},"headline":"An instructional tutorial on the Jackson JSON Java Parser API.","datePublished":"2023-08-06T20:33:05+00:00","dateModified":"2024-03-16T15:46:36+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/an-instructional-tutorial-on-the-jackson-json-java-parser-api\/"},"wordCount":1009,"commentCount":0,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/an-instructional-tutorial-on-the-jackson-json-java-parser-api\/","url":"https:\/\/www.silicloud.com\/blog\/an-instructional-tutorial-on-the-jackson-json-java-parser-api\/","name":"An instructional tutorial on the Jackson JSON Java Parser API. - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2023-08-06T20:33:05+00:00","dateModified":"2024-03-16T15:46:36+00:00","description":"Jackson JSON Java Parser is highly favored and also utilized in the Spring framework. However, the Java JSON Processing API is not","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/an-instructional-tutorial-on-the-jackson-json-java-parser-api\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/an-instructional-tutorial-on-the-jackson-json-java-parser-api\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/an-instructional-tutorial-on-the-jackson-json-java-parser-api\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"An instructional tutorial on the Jackson JSON Java Parser API."}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/2e83cc6ab9f60d36921c2d0f9f280f4a","name":"Noah Thompson","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/350e537e1530ede2762ee0237e877d6693f4f7163ab4f303202cc9a6b27b6cb4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/350e537e1530ede2762ee0237e877d6693f4f7163ab4f303202cc9a6b27b6cb4?s=96&d=mm&r=g","caption":"Noah Thompson"},"url":"https:\/\/www.silicloud.com\/blog\/author\/noahthompson\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/1457","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=1457"}],"version-history":[{"count":0,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/1457\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=1457"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=1457"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=1457"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}