Found 206 Articles for JSON

How to implement a custom serializer using the Jackson library in Java?

raja
Updated on 06-Jul-2020 11:52:25

931 Views

The Jackson API provides a number of methods to work with JSON data. By using Jackson API, we can convert Java objects to JSON string and reform the object from the JSON string. We can implement a custom serializer using the StdSerializer class and need to override the serialize(T value, JsonGenerator gen, SerializerProvider provider) method, the first argument value represents value to serialize(can not be null), the second argument gen represents generator used to output resulting Json content and the third argument provider represents provider that can be used to get serializers for serializing objects value.Syntaxpublic abstract void serialize(T value, JsonGenerator gen, SerializerProvider provider) throws IOExceptionExampleimport java.io.*; ... Read More

How to convert Java object to JSON using Jackson library?

Maruthi Krishna
Updated on 06-Sep-2023 11:49:04

45K+ Views

JSON or JavaScript Object Notation is a lightweight text-based open standard designed for human-readable data interchange. Conventions used by JSON are known to programmers, which include C, C++, Java, Python, Perl, etc.There are several Java libraries available to handle JSON objects. Jackson is a simple java based library to serialize java objects to JSON and vice versa.Converting Java object to JSONThe ObjectMapper class of the Jackson API in Java provides methods to convert a Java object to JSON object and vice versa.The writeValueAsString() method of this class accepts a JSON object as a parameter and returns its respective JSON StringTherefore, ... Read More

Convert CSV to JSON using the Jackson library in Java?

raja
Updated on 06-Jul-2020 11:44:03

7K+ Views

A Jackson is a Java JSON API that provides several different ways to work with JSON. We can convert CSV data to JSON data using the CsvMapper class, it is specialized ObjectMapper, with extended functionality to produce CsvSchema instances out of POJOs. We can use the reader() method for constructing ObjectReader with default settings. In order to convert this, we need to import the com.fasterxml.jackson.dataformat.csv package.In the below example, convert a CSV to JSON.Exampleimport java.io.*; import java.util.*; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.dataformat.csv.*; public class CsvToJsonTest {    public static void main(String args[]) throws Exception {       File input = new File("input.csv");       try { ... Read More

How to deserialize a JSON array to list generic type in Java?

raja
Updated on 06-Jul-2020 11:38:01

1K+ Views

The Gson library provides a class called com.google.gson.reflect.TypeToken to store generic types by creating a Gson TypeToken class and pass the class type. Using this type, Gson can able to know the class passed in the generic class.Syntaxpublic class TypeToken extends ObjectWe can deserialize a JSON array to a generic type of list in the below exampleExampleimport java.lang.reflect.Type; import java.util.*; import com.google.gson.*; import com.google.gson.reflect.*; public class JSONArrayToListTest {    public static void main(String args[]) throws Exception {       String jsonStr = "[{\"name\":\"Adithya\", \"course\":\"Java\"}, " + "{\"name\":\"Ravi\", \"course\":\"Python\"}]";       Type listType = new TypeToken() {}.getType();       List students = ... Read More

Convert XML to POJO using the Jackson library in Java?

raja
Updated on 14-Feb-2020 10:12:44

7K+ Views

The JSON Jackson is a library for Java. It has very powerful data binding capabilities and provides a framework to serialize custom java objects to JSON and deserialize JSON back to Java object. We can also convert an XML format to the POJO object using the readValue() method of the XmlMapper class.Syntaxpublic T readValue(XMLStreamReader r, Class valueType) throws IOExceptionExampleimport com.fasterxml.jackson.dataformat.xml.*; public class XMLToPOJOTest {    public static void main(String args[]) throws Exception {       try {          XmlMapper xmlMapper = new XmlMapper();          Person pojo = xmlMapper.readValue(getXmlString(), Person.class);          System.out.println(pojo); ... Read More

Convert POJO to XML using the Jackson library in Java?

raja
Updated on 06-Jul-2020 09:24:08

5K+ Views

A Jackson is a Java-based library and it can be useful to convert Java objects to JSON and JSON to Java Object. A Jackson API is faster than other API, needs less memory area and is good for the large objects. We convert a POJO to XML format using the writeValueAsString() method of XmlMapper class and we need to pass the corresponding POJO instance as an argument to this method.Syntaxpublic String writeValueAsString(Object value) throws JsonProcessingExceptionExampleimport com.fasterxml.jackson.dataformat.xml.*; public class POJOToXmlTest {    public static void main(String args[]) throws Exception {       try {          XmlMapper xmlMapper = new ... Read More

How to convert MySQL DATETIME value to JSON format in JavaScript?

AmitDiwan
Updated on 07-Oct-2019 10:49:22

473 Views

To convert, use JSON.stringify(). Following is the code to convert MySQL DATETIME value to JSON format in JavaScript − var mySQLDateTime = new Date("Fri Sep 06 2019 22 −54 −48 "); var yearValue = mySQLDateTime.getFullYear(); var dateValue = mySQLDateTime.getDate(); var monthValue=mySQLDateTime.getMonth(); var hour=mySQLDateTime.getHours(); var minutes=mySQLDateTime.getMinutes(); var second=mySQLDateTime.getSeconds(); jsonObject={"year" −yearValue,"month" :monthValue,"DateValue" :dateValue,"Hour" :hour ,"Minutes" :minutes,"Second" :second}; var dateJsonObject = JSON.stringify(jsonObject); document.write(dateJsonObject); The screenshot of the code is as follows −This will produce the following output −{"year" :2019,"month" :8,"DateValue" :6,"Hour" :22,"Minutes" :54,"Second" :48}The snapshot of the output is as follows −

How to parse a JSON without duplicate keys using Gson in Java?

raja
Updated on 06-Jul-2020 08:51:36

914 Views

A Gson is a JSON library for Java, which is created by Google. By using Gson, we can generate JSON and convert JSON to Java objects. We can create a Gson instance by creating a GsonBuilder instance and calling with the create() method. We can parse a JSON without duplicate keys using the TypeToken class. If we want to create a type literal for Map, we can create an empty anonymous inner class. If we try to insert a duplicate key, it will generate an error at runtime, "Exception in thread "main" com.google.gson.JsonSyntaxException: duplicate key"Syntaxpublic class TypeToken extends ObjectExampleimport java.lang.reflect.Type; import java.util.Map; import ... Read More

How can we format a date using the Jackson library in Java?

raja
Updated on 06-Jul-2020 08:47:42

2K+ Views

A Jackson is a Java based library and it can be useful to convert Java objects to JSON and JSON to Java Object. A Jackson API is faster than other API, needs less memory area and is good for the large objects. We can format a date using the setDateFormat() of ObjectMapper class. This method can be used for configuring the default DateFormat when serializing time values as Strings and deserializing from JSON Strings.Syntaxpublic ObjectMapper setDateFormat(DateFormat dateFormat)Exampleimport java.io.*; import java.text.*; import java.util.*; import com.fasterxml.jackson.databind.*; public class JacksonDateformatTest {    final static ObjectMapper mapper = new ObjectMapper();    public static void main(String[] args) ... Read More

How to serialize a map using the flexjson library in Java?

raja
Updated on 06-Jul-2020 08:23:21

239 Views

The Flexjson is a lightweight library for serializing and deserializing Java objects into and from JSON format. We can also serialize a Map using the serialize() method of JSONSerializer class, it performs a shallow serialization of the target instance.Syntaxpublic String serialize(Object target)Exampleimport flexjson.JSONSerializer; import java.util.*; public class JsonSerializeMapTest {    public static void main(String[] args) {       JSONSerializer serializer = new JSONSerializer().prettyPrint(true);       Student student = new Student("Adithya", "Sai", 28, "Hyderabad");       Map map = new HashMap();       map.put("Student1", "Raja");       map.put("Student2", "Ravi");       map.put("my_student", student);       String jsonStr = ... Read More

Advertisements