How To Create JSON Object In Java
Last Updated : Mar 11, 2024
IN - Java JSON | Written & Updated By - Dikshita

In this article we will show you the solution of how to create JSON object in java, a lightweight data exchange format, JSON (JavaScript Object Notation), is used most commonly for client-server communication.
There is a cross-linguistic component as well as easy reading and writing.
You can use several types of JSON values as JSON values, including another JSON object, an array, an integer, a text, a boolean (true/false), or null.
This tutorial will show you how to produce, alter, and parse JSON using the JSON-Java library.
We will now discuss the idea of how to create a json object in java with an example.
Step By Step Guide On How To Create JSON Object In Java :-
Code 1
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.StringWriter;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.json.JsonStructure;
import javax.json.JsonWriter;
public class talkerscode {
public static void main(string args[])
throws Filenotfoundexception
{
reader = Json.create(
new filereader("demo.txt"));
JsonStructure jsonst = reader.read();
StringWriter stWriter = new StringWriter();
try (JsonWriter jsonWriter
= Json.createWriter(stWriter)) {
jsonWriter.writeObject((JsonObject)jsonst);
}
String jsonData = stWriter.toString();
System.out.println(jsonData);
}
}
Code 2
package com.javaapi.json.examples;
import java.io.StringWriter;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.json.JsonWriter;
public class talkerscode {
public static void main(String a[]){
JsonObjectBuilder jsonBuilder = Json.createObjectBuilder();
jsonBuilder.add("emp_name", "Amruta");
jsonBuilder.add("emp_id", 2507);
jsonBuilder.add("salary", 80000);
JsonObject empObj = jsonBuilder.build();
Stringwriter strwtr = new Stringwriter();
Jsonwriter jsonwtr = Json.createwriter(strwtr);
jsonwtr.writeObject(empObj);
jsonwtr.close();
System.out.println(strwtr.toString());
}
}
- In order to get started, we import the required classes: FileNotFoundException, FileReader, StringWriter, Json, JsonObject, JsonReader, JsonStructure, and JsonWriter.
- We then declare TalkersCode as our main class.
- Using the Json class's createReader function, we create a JsonReader object with the name reader and read data from the sample.txt file.
- The information is then read from the reader object and created into a JsonStructure called jsonst.
- A StringWriter called stWriter is then created.
- The Json class's createWriter method, which writes JsonObject to the stWriter, is used to create a JsonWriter with the name jsonWriter using a try-with-resources block.
- We use the jsonWriter to write the JsonObject contained in the jsonst.
- When finished, we shut off the JsonWriter.
- The data in the stWriter is subsequently converted to String format.
- The jsonData is then printed on the console.
- This program prints the JsonObject included in the input JSON file sample.txt as a String.
Conclusion :-
As a result, we have successfully learned how to create json object in java with an example.
A collection of unordered name/value pairs is referred to as a JSONObject.
The string's external form has curly braces around it, commas separating names from values, and colons separating names from values.
I hope this article on how to create JSON object in java helps you and the steps and method mentioned above are easy to follow and implement.













