Found 208 Articles for JSON

How to access the JSON fields, arrays and nested objects of JsonNode in Java?

Aishwarya Naglot
Updated on 13-May-2025 17:15:34

33K+ Views

What are JSON and JsonNode? JSON is a format that is used to store data and exchange data between a server and a client. To know more about JSON, you can refer to the JSON Tutorial. A JsonNode is Jackson's tree model for JSON. To read JSON into a JsonNode with Jackson by creating an ObjectMapper instance and call the readValue() method. We can access a field, array or nested object using the get() method of the JsonNode class. We can return a valid string representation using the asText() method and ... Read More

How to implement custom JSON de-serialization with Gson in Java?

Aishwarya Naglot
Updated on 12-May-2025 10:12:16

671 Views

Deserialization is the process of converting JSON data back into Java objects. Gson provides a simple way to do this using the fromJson() method. Custom JSON de-serialization with Gson Gson is a Java library developed by Google to convert Java objects into their JSON format and vice versa. Custom JSON is a way we can modify or extend the standard JSON format so that it can suit our specific needs. To use the Gson library, we need to add the Gson library to our project. If you are using Maven, add this to your pom.xml file: com.google.code.gson gson 2.8.9 ... Read More

Importance of a JSONTokener in Java?

Aishwarya Naglot
Updated on 12-May-2025 11:03:10

2K+ Views

If you have ever worked with JSON in Java, you might have used classes like JSONObject, JSONArray, JsonParser, etc. These classes are part of the org.json library. We use these classes to build JSON or parse JSON. But behind the scenes, these classes use a class called JSONTokener. In this article, we will learn about the JSONTokener class and its importance in Java. What is JSONTokener? The JSONTokener class is like a tool that reads a JSON string step by step, breaking it into smaller parts called tokens. It is like a helper that makes it easier to understand and ... Read More

How can we add a JSONArray within JSONObject in Java?

raja
Updated on 04-Jul-2020 08:50:58

5K+ Views

A JSONObject can parse text from a String to produce a map-like object and a JSONArray can parse text from a String to produce a vector-like object. We can also add a JSONArray within JSONObject by first creating a JSONArray with few items and add these array of items to the put() method of JSONObject class.Syntaxpublic JSONObject put(java.lang.String key, java.util.Collection value) throws JSONExceptionExampleimport org.json.*; public class AddJSONArrayTest {    public static void main(String[] args) throws JSONException {       JSONArray array = new JSONArray();       array.put("INDIA");       array.put("AUSTRALIA");       array.put("ENGLAND");       JSONObject obj = ... Read More

How to implement custom JSON serialization with Gson in Java?

Aishwarya Naglot
Updated on 12-May-2025 10:38:49

1K+ Views

Custom JSON serialization Custom JSON Serialization means defining how your Java objects should be converted to JSON format. This is useful when you want to control the JSON output, such as changing field names, excluding certain fields, or formatting the data in a specific way. In this tutorial, we will create a custom serializer for a Java object using the Gson library. We will define a class Person with fields for name and age, and then create a custom serializer to control how this object is converted to JSON. To use Gson library, we need to add the Gson library ... Read More

How to convert XML to JSON array in Java?

Aishwarya Naglot
Updated on 12-May-2025 10:31:12

9K+ Views

Xml is a markup language that is used to store or transport data. It uses tags to define the data. There are libraries also available for converting XML to JSON. Those are: org.json Jackson Gson Let's learn how to convert XML to JSON using various libraries. Converting XML to JSON using org.json For converting XML to JSON, we will use the org.json library. It is a simple library that is used for converting XML to JSON and vice versa. We can use the XML.toJSONObject() method to ... Read More

How to write a JSON string to file using the Gson library in Java?

Aishwarya Naglot
Updated on 12-May-2025 10:22:09

2K+ Views

Let's learn how to write a JSON string to a file using the Gson library in Java. If you are not familiar with JSON, you can refer to JSON Overview. Gson is a library that can be used to convert Java Objects to a JSON representation. To know more about Gson, refer to the Gson tutorial. GsonBuilder Class The primary class to use is Gson, which we can create by calling the new Gson(), and the GsonBuilder class can be used to create a Gson instance. We can write a JSON string to a file using the toJson() method of ... Read More

How to get all the keys of a JSON object using GSON in Java?

Aishwarya Naglot
Updated on 22-Apr-2025 18:31:43

4K+ Views

JSON is the data interchange format that stores values in key-value pairs. (To learn more about JSON, you can refer to the JSON tutorial). So, as we have key and value in JSON, we will try to get all those keys using the Gson library. Gson Library: Retrieving all the keys of a JSON Gson is a third-party Java library developed by Google. It is used for converting Java objects to JSON and vice versa. To know more about Gson, refer to the Gson tutorial. We can use the keySet() method of JsonObject class to get all the keys of a ... Read More

How to serialize and de-serialize generic types using the Gson library in Java?

Aishwarya Naglot
Updated on 12-May-2025 09:15:57

2K+ Views

If a Java class is a generic type, and we are using it with the Gson library for JSON serialization and deserialization, the TypeToken class from com.google.gson.reflect package is used for preserving the generic type information at runtime. What are Generic Types in Java? Generic types in Java mean we don't set any method or class to a specific type, like int, String, etc. Instead, we use a type parameter (like T) to represent the type. This allows us to create classes and methods that can operate on objects of different types, and also provides compile-time safety at the same time. ... Read More

Pretty print JSON using Jackson library in Java?

Aishwarya Naglot
Updated on 12-May-2025 12:30:00

2K+ Views

A Jackson API is a Java-based library, and it can be useful to convert Java objects to JSON and JSON to Java objects. A Jackson API is faster than other API, needs less memory, and is good for large objects. We can process a JSON in three different ways: Streaming API, Tree Model, and Data Binding. We can pretty print JSON using the writerWithDefaultPrettyPrinter() of the ObjectMapper class, which is a factory method for constructing an ObjectWriter that will serialize objects using the default pretty printer for indentation. In this article, we will learn how to pretty print JSON using the Jackson ... Read More

Advertisements