
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
When to Use JSONStringer in Java
A JSONStringer provides a convenient way of producing JSON text, and it can strictly follow to JSON syntax rules. Each instance of JSONStringer can produce one JSON text. A JSONStringer instance provides a value method for appending values to the text and a key-method for adding keys before values in objects.
We have array () and endArray() methods that create and bind array values, and object() and endObject () methods that create and bind object values. It is part of the org.json library. It is generally used in Android and lightweight Java programs.
In this article, we will learn when to use a JSONStringer in Java.
When to use a JSONStringer in Java
We need JSONStringer in many situations. Below are a few examples where we can use JSONStringer.
When we need to create a JSON object dynamically.
Creating a dynamic JSON object means the size of the object is not fixed. I can grow or even shrink as per the requirement.
For example, we are getting active data from some source, and we are not sure about how much data we may receive. In this case, we can use JSONStringer to create a JSON object dynamically.
When we need to create a JSON object with nested objects or arrays.
A Nested object means an object is present inside another object.
Suppose you are given a document that has values and sub-values. In this case, we will need nested JSON objects to store those values and their sub-values. So we can use JSONStringer to create a JSON object with nested objects.
When we need to create a JSON object with arrays or complex data types.
Creating a JSON object with complex data types means adding other data types like arrays, lists, maps, etc. to a JSON object.
Let's say you are asked to save a list of the favorite movies of a user. We can't simply save them in a JSON object. We need to create a JSON array and then add the movies to that array. So we can use JSONStringer to create a JSON object with arrays.
When the keys and values are unknown at compile time.
The key and value pairs are not known at compile time; we don't have their values yet, but we may get them at runtime.
In some cases, we may not know the keys and values at compile time. In this case, we can use JSONStringer to create a JSON object with unknown keys and values.
When you don't want to use any third-party libraries.
JSONStringer is part of the org.json library. It is a lightweight library and does not require any third-party libraries.
Example 1
The following program demonstrates the use of JSONStringer when we have a JSON object with nested objects.
import org.json.JSONStringer; public class JSONStringerExample { public static void main(String[] args) { String jsonString = new JSONStringer() .object() .key("name").value("Aish") .key("age").value(22) .key("skills") .object() .key("primary").value("Java") .key("secondary").value("C++") .key("other").value("JavaScript") .endObject() .key("address") .object() .key("city").value("Somewhere") .key("zip").value("10001") .endObject() .endObject() .toString(); System.out.println(jsonString); } }Following is the output of the above program -
{ "name": "Aish", "age": 22, "skills": { "primary": "Java", "secondary": "C++", "other": "JavaScript" }, "address": { "city": "Somewhere", "zip": "10001" } }
Example 2
The following is an example of creating a JSON object with arrays. Here, we will create a JSON object with arrays.
import org.json.JSONStringer; import org.json.JSONException; public class JSONStringerExample2 { public static void main(String[] args) throws JSONException { String jsonString = new JSONStringer() .object() .key("name").value("Aish") .key("age").value(22) .key("skills") .array() .value("Java") .value("C++") .value("JavaScript") .endArray() .endObject() .toString(); System.out.println(jsonString); } }
Following is the output of the above program -
{ "name": "Aish", "age": 22, "skills": [ "Java", "C++", "JavaScript" ] }