Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20

 

Print

Write Simple JSON in Java

Using JSonObject, we can create a simple Json and write Json content to a file. This article explains how to create a simple Json content. Create a maven based project and add the following dependencies in the pom.xml file. 

 

<dependency>
	<groupId>org.codehaus.woodstox</groupId>
	<artifactId>woodstox-core-asl</artifactId>
	<version>4.1.4</version>
</dependency>
<dependency>
	<groupId>javax.xml.stream</groupId>
	<artifactId>stax-api</artifactId>
	<version>1.0-2</version>
	<scope>compile</scope>
</dependency>
<dependency>
	<groupId>org.glassfish</groupId>
	<artifactId>javax.json</artifactId>
	<version>1.0.4</version>
</dependency>

We create the following JSON and write the JSON to a file "test.json"

{
	"TestName": "Login",
	"TestModule": "Dashboard",
	"TestType": "Regression",
	"TestSteps": [{
		"Id": 1,
		"Step": "Open browser"
	},
	{
		"Id": 2,
		"Step": "Enter user name"
	},
	{
		"Id": 3,
		"Test": "Enter user password"
	},
	{
		"Id": 4,
		"Step": "Click on Login button"
	}],
	"Verification": "Logout link should display",
	"Note": ""
}

write the following code in the class "CreateSimpleJason.java"

package com.example.Json;

import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class CreateSimpleJason {

    public static void main(String[] args)
    {
    CreateJason();
    }

    public static void CreateJason()
    {
                JsonObject value = Json.createObjectBuilder()
                .add("TestName", "Login")
                .add("TestModule", "Dashboard")
                .add("TestType", "Regression")
                .add("TestSteps", Json.createArrayBuilder()
                                .add(Json.createObjectBuilder().add("Id",1).add("Step", "Open browser"))
                                .add(Json.createObjectBuilder().add("Id",2).add("Step", "Enter user name"))
                                .add(Json.createObjectBuilder().add("Id",3).add("Step", "Enter user password"))
                                .add(Json.createObjectBuilder().add("Id",4).add("Step", "Click on Login button")))
                        .add("Verification", "Logout link should display")
                        .add("Note", "")
                        .build();
        System.out.println( value.toString());
        JsonWriter writer = null;
        try {
            writer = Json.createWriter(new FileOutputStream(new File("test.json")));
            writer.writeObject(value);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

}

Run the code and see the console result below. A file "test.json" is created.

{
	"TestName": "Login",
	"TestModule": "Dashboard",
	"TestType": "Regression",
	"TestSteps": [{
		"Id": 1,
		"Step": "Open browser"
	},
	{
		"Id": 2,
		"Step": "Enter user name"
	},
	{
		"Id": 3,
		"Test": "Enter user password"
	},
	{
		"Id": 4,
		"Step": "Click on Login button"
	}],
	"Verification": "Logout link should display",
	"Note": ""
}