All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Create JSON Object In Java

Last Updated : Mar 11, 2024

How To Create JSON Object In Java

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());
 }
}
  1. In order to get started, we import the required classes: FileNotFoundException, FileReader, StringWriter, Json, JsonObject, JsonReader, JsonStructure, and JsonWriter.
  2. We then declare TalkersCode as our main class.
  3. Using the Json class's createReader function, we create a JsonReader object with the name reader and read data from the sample.txt file.
  4. The information is then read from the reader object and created into a JsonStructure called jsonst.
  5. A StringWriter called stWriter is then created.
  6. 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.
  7. We use the jsonWriter to write the JsonObject contained in the jsonst.
  8. When finished, we shut off the JsonWriter.
  9. The data in the stWriter is subsequently converted to String format.
  10. The jsonData is then printed on the console.
  11. 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.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪