JSON File Reading- error | Selenium Forum
S
Soham Posted on 18/11/2020

Hey Ashish,

I just created a small program to read Json file after going through videos and I am expecting to get NULL values as per my JSON test data. But I did not get any null values as you got in training session video. I tired to run program in debug mode and could see it was reading all values like mincost, maxcost from JSON file. I am pasting screenshot, my code below and also the JSON file. Please help why NULL values are not printed in console.

****CODE****

import java.io.FileReader;
import java.io.IOException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class ReadJasonFile {

public static void main(String[] args) throws IOException, ParseException {

String path= System.getProperty("user.dir")+"\\TestData.json";
FileReader flreader= new FileReader(path);
JSONParser jparser=new JSONParser();
JSONObject entireJsonObject= (JSONObject) jparser.parse(flreader);
JSONArray firstTabData=(JSONArray) entireJsonObject.get("testdata");
for(int i=0;i<firstTabData.size();i++)
{
JSONObject firstTabDetailData= (JSONObject) firstTabData.get(i);
String testName= (String) firstTabDetailData.get("testName");
System.out.println();
System.out.println(testName);
JSONArray currentTestData= (JSONArray) firstTabDetailData.get("data");
System.out.println(currentTestData.size());
for(int j=0;j<currentTestData.size();j++) {
JSONObject testDataSet= (JSONObject) currentTestData.get(j);
String runmode= (String) testDataSet.get("runmode");
String browser=(String) testDataSet.get("browser");
String username=(String) testDataSet.get("username");
String password=(String) testDataSet.get("password");
System.out.println(runmode+"-- "+browser+" --"+username+"-- "+password);

}}}}

***JSON FILE**

{
"testdata":[
{
"testName": "LoginTest",
"data" : [
{
"runmode": "y",
"browser": "chrome",
"username": "abc@gmail.com",
"password": "pass1234"
},
{
"runmode": "n",
"browser": "mozilla",
"username": "abc@gmail.com",
"password": "pass1234"
},
{
"runmode": "y",
"browser": "edge",
"username": "abc@gmail.com",
"password": "pass1234"
}
]
},
{
"testName": "PurchaseTest",
"data" : [
{
"runmode": "y",
"maxcost": "100",
"browser": "chrome",
"username": "abc@gmail.com",
"password": "pass1234"
},
{
"runmode": "n",
"maxcost": "200",
"browser": "mozilla",
"username": "abc@gmail.com",
"password": "pass1234"
},
{
"runmode": "y",
"maxcost": "300",
"browser": "edge",
"username": "abc@gmail.com",
"password": "pass1234"
}
]
},

{
"testName": "ExpenseTest",
"data" : [
{
"runmode": "y",
"mincost": "1000",
"browser": "chrome",
"username": "abc@gmail.com",
"password": "pass1234"
},
{
"runmode": "n",
"mincost": "2000",
"browser": "mozilla",
"username": "abc@gmail.com",
"password": "pass1234"
},
{
"runmode": "y",
"mincost": "3000",
"browser": "ÏE",
"username": "abc@gmail.com",
"password": "pass1234"
}

]
}
]
}

 


A
Ashish Thakur Replied on 19/11/2020

For which line/ouput are you expecting null values


S
Soham Replied on 19/11/2020

Hello Ashish,

 

I am only printing 4 values as - System.out.println(runmode+"-- "+browser+" --"+username+"-- "+password);

But JSON file has other key values down in 'PurchaseTest' and 'ExpenseTes't as 'maxcost' and 'mincost'.. For these I was expecting NULL values before I start using SET concept that you told a a solution. But I am not even getting NULL, so hat was my query around

testName": "PurchaseTest",
"data" : [
{
"runmode": "y",
"maxcost": "100",
"browser": "chrome",
"username": "abc@gmail.com",
"password": "pass1234"

 

and

 

"testName": "ExpenseTest",
"data" : [
{
"runmode": "y",
"mincost": "1000",
"browser": "chrome",
"username": "abc@gmail.com",
"password": "pass1234"


A
Ashish Thakur Replied on 20/11/2020

String runmode= (String) testDataSet.get("runmode");
String browser=(String) testDataSet.get("browser");
String username=(String) testDataSet.get("username");
String password=(String) testDataSet.get("password");

System.out.println(runmode+"-- "+browser+" --"+username+"-- "+password);

You have extracted only 4 values. You \have not extracted mincost and max cost and printed

So why would it print it?


S
Soham Replied on 20/11/2020

Hello Ashish, Thanks for replying but my point is that I am expecting NULL values here, but not getting that. Below is extact from your training video. Hope this helps in understanding what I am trying to ask!

 

 

Responsive image