
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create a JSON using Jackson Tree Model in Java?
In the Jackson library, we can use the Tree Model to represent the JSON structure and perform the CRUD operations via JsonNode. This Jackson Tree Model is useful, especially in cases where a JSON structure does not map to Java classes. We can create a JSON in the Jackson library using the JsonNodeFactory, it can specify the methods for getting access to Node instances as well as the basic implementation of the methods. We can use the set() and put() methods of ObjectNode class to populate the data.
Syntax
public class JsonNodeFactory extends Object implements Serializable
Example
import java.io.*; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.node.*; public class JacksonTreeModelTest { public static void main(String args[]) throws IOException { JsonNodeFactory factory = new JsonNodeFactory(false); ObjectMapper mapper = new ObjectMapper(); ObjectNode employee = factory.objectNode(); employee.put("empId", 125); employee.put("firstName", "Raja"); employee.put("lastName", "Ramesh"); ArrayNode technologies = factory.arrayNode(); technologies.add("Python").add("Java").add("SAP"); employee.set("technologies", technologies); System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(employee)); } }
Output
{ "empId" : 125, "firstName" : "Raja", "lastName" : "Ramesh", "technologies" : [ "Python", "Java", "SAP" ] }
- Related Questions & Answers
- How to create a JSON Array using Object Model in Java?
- How to create a JSON Object using Object Model in Java?
- How to parse a JSON to Gson Tree Model in Java?
- How to map the JSON data with Jackson Object Model in Java?
- JSON Schema Support using Jackson in Java?
- How to convert Java object to JSON using Jackson library?
- How to convert a JSON to Java Object using the Jackson library in Java?
- How to search a value inside a JSON file using Jackson in Java?
- Pretty print JSON using Jackson library in Java?
- How to convert a JSON object to an enum using Jackson in Java?
- How can we convert a JSON array to a list using Jackson in Java?
- Convert JSON to/from Map using Jackson library in Java?
- Convert CSV to JSON using the Jackson library in Java?
- How to convert a List to JSON array using the Jackson library in Java?
- How can we change a field name in JSON using Jackson in Java?
Advertisements