Found 208 Articles for JSON

How can we convert a list to the JSON array in Java?

Aishwarya Naglot
Updated on 22-Apr-2025 14:25:07

33K+ Views

Converting a list to a JSON array is one of the common tasks in Java. Let's see how to convert a list to a JSON array in Java. If you do not know what JSON is, then you can read the JSON tutorial. The following are various ways to convert a list to a JSON array in Java: Manual construction of JSON array Using Jackson library Using Gson library Using org.json library Let's learn the process of each of them one by ... Read More

How can we decode a JSON object in Java?

raja
Updated on 04-Jul-2020 06:27:24

3K+ Views

A JSON is a lightweight, text-based and language-independent data exchange format. A JSON can represent two structured types like objects and arrays. We can decode a JSON object using JSONObject and JSONArray from json.simple API. A JSONObject works as a java.util.Map whereas JSONArray works as a java.util.List.In the below example, we can decode a JSON object.Exampleimport org.json.simple.*; import org.json.simple.parser.*; public class JSONDecodingTest {    public static void main(String[] args) {       JSONParser parser = new JSONParser();       String str = "[ 0 , {\"1\" : { \"2\" : {\"3\" : {\"4\" : [5, { \"6\" : { \"7\" : 8 } } ] } ... Read More

How can we encode a JSON object in Java?

raja
Updated on 04-Jul-2020 05:58:28

3K+ Views

A JSONObject is a subclass of java.util.HashMap where no order is provided. We can also use the strict ordering of elements as well with the help of the JSONValue.toJSONString(map) method i.e. by the implementation of java.util.LinkedHashMap.We can encode a JSON object in the below two examples.Example import java.util.*; import org.json.simple.JSONObject; public class JSONEncodingTest {    public static void main(String[] args) {       Map dataMap = new HashMap();       dataMap.put("Name", "Adithya");       dataMap.put("Age", new Integer(25));       dataMap.put("Salary", new Double(25000.00));       dataMap.put("Employee Id", new Integer(115));       dataMap.put("Company", "TutorialsPoint");       JSONObject ... Read More

How can we parse a nested JSON object in Java?

Aishwarya Naglot
Updated on 22-Apr-2025 16:28:52

15K+ Views

Sometimes, we need to parse a nested JSON object, which means an object inside another object. In this article, let's learn about how to parse a nested JSON object in Java. If you don't know about JSON, refer JSON overview. Parsing a Nested JSON Object in Java? We can achieve this using some of the popular Java libraries. Those are listed below: Using org.json library : We will use jsonObject.getJSONObject method to parse a nested JSON object. Using Gson library : We will use JsonParser.parseString(json).getAsJsonObject(). Using Jackson library : In this, We will use ObjectMapper.readTree(json) method. Now, ... Read More

Convert a JSON object to XML format in Java?

Aishwarya Naglot
Updated on 21-Apr-2025 18:54:40

7K+ Views

In this article, we will learn how to convert a JSON object into XML format in Java. But first, let’s understand what JSON and XML are. JSON is a lightweight data-interchange format, and the format of JSON is like a key-value pair. XML (Extensible Markup Language) is also a way to store data, but unlike JSON, it uses tags to make the proper structure. Why Do we Convert JSON to XML? There can be multiple situations where we would need to convert JSON to XML. Here are some of the reasons: XML is ... Read More

Convert a JSON String to Java Object using the json-simple library in Java?

Aishwarya Naglot
Updated on 21-Apr-2025 19:35:29

2K+ Views

In this article, we will learn how to convert a JSON string to a Java object using the json.simple library. JSON stands for JavaScript Object Notation. It's a simple format that stores data in key-value pairs. It is used for sharing data between a server and a web app. To learn more about JSON, you can refer to the JSON tutorial. In Java, we can use a library called json.simple to handle JSON data. So, using this, we can easily convert a JSON string into a Java object. Why Convert JSON String to Java Object? There are many reasons to ... Read More

How to construct a JSON object from a subset of another JSON object in Java?

Aishwarya Naglot
Updated on 21-Apr-2025 19:33:12

3K+ Views

In this article, we will learn how to construct a JSON object from a subset of another JSON object in Java. If you are not familiar with JSON and its usage in Java, you can refer to our JSON Overview tutorial. JSON Object From a Subset of Another Object There is more than one way to construct a JSON object from a subset of another JSON object in Java. Some of the popular libraries that we can use are - Using org.json library Using Gson library Using ... Read More

How can we merge two JSON arrays in Java?

Aishwarya Naglot
Updated on 22-Apr-2025 18:35:33

8K+ Views

In this article, we will be learning how to merge two JSON arrays in Java. Let's understand step by step. First, if you are not familiar with JSON, refer JSON overview. How to Merge Two JSON Arrays in Java? There are multiple ways to merge two JSON arrays in Java. Some of them are: Using org.json library: We will use JSONArray.put() method to merge two JSON arrays. Using Gson library: We will use JsonArray.add() method to merge two JSON arrays. Using Jackson library: We will use ObjectMapper.createArrayNode() ... Read More

How can we convert a map to the JSON object in Java?

raja
Updated on 04-Jul-2020 05:51:35

9K+ Views

The JSON is a lightweight, text-based and language-independent data exchange format. The JSON can represent two structured types like objects and arrays. An object is an unordered collection of key/value pairs and an array is an ordered sequence of values. We can convert a Map to JSON object using the toJSONString() method(static) of org.json.simple.JSONValue. It has two important static methods: writeJSONString() method to encode an object into JSON text and write it out, escape() method to escape the special characters and escape quotes,  \, /, \r, , \b, \f, \t.Exampleimport java.util.*; import org.json.simple.JSONValue; public class ConvertMapJSONTest {    public static void main(String[] args) {       ... Read More

How can we merge two JSON objects in Java?

raja
Updated on 04-Jul-2020 05:40:36

17K+ Views

A JSON is a lightweight data-interchange format and the format of JSON is a key with value pair. The JSONObject can parse a text from a String to produce a map-like object and supports java.util.Map interface. We can use org.json.simple.JSONObject to merge two JSON objects in Java.We can merge two JSON objects using the putAll() method (inherited from interface java.util.Map) in the below program.Exampleimport java.util.Date; import org.json.simple.JSONObject; public class MergeJsonObjectsTest {    public static void main(String[] args) {       JSONObject jsonObj = new JSONObject(); // first json object       jsonObj.put("Name", "Adithya");       jsonObj.put("Age", 25);     ... Read More

Advertisements