-2

I have a for loop which iterates and generates key value pairs for different employees.

I need to create a JSON array like below and write it to a JSON file at the end.

I am having trouble figuring out the ideal way to code it (JSON Objects -> JSON Array -> JSON file?).

I am open to use json-simple/GSON.

Desired JSON file format:

[
        {
            "employeeFirstName": "Mark",
            "employeeLastName": "Williams",
            "employeeDepartment": "Sales",
        },
        {
            "employeeFirstName": "John",
            "employeeLastName": "Carpenter",
            "employeeDepartment": "Accounts",
        },
        {
            "employeeFirstName": "David",
            "employeeLastName": "Hunter",
            "employeeDepartment": "Marketing",
        },
]

I tried using a JSONObject and add it to a JSONArray. But, couldn't figure how to code it for iterations.

My current Java class:

public class Test {

public void createEmployeesJSONArrayFile(ITestContext iTestContext) {
                for (ITestResult testResult : iTestContext.getFailedTests().getAllResults()) {
                    System.out.println("employeeFirstName: " + testResult.getEmployeeFirstName()));
                    System.out.println("employeeLastName: " + testResult.getEmployeeLastName());
                    System.out.println("employeeDepartment: " + testResult.getEmployeeDepartment());
                }
 }
}

What is the simplest or ideal way to achieve this?

AutoTester999
  • 528
  • 1
  • 6
  • 25
  • 1
    If you pass gson a list/set, it will serialise it to a json array for you (I’d guess json simple is the same). Where is your code that uses gson/json simple and what does that do at present? – BeUndead Jul 11 '21 at 00:01

3 Answers3

1

A simple way to achieve this would be to use Gson, an API provided by Google. You could write the Collection of ITestResult objects to a file. The toJson function will take the Collection of ITestResult objects and write them to the the given Appenable object, which in this case is a BufferedWriter which points to a file.

(untested, one sec, not at workstation)

Collection<ITestResult> results = iTestContext.getFailedTests().getAllResults();

new GsonBuilder()
    .create()
    .toJson(results, Files.newBufferedWriter(Paths.get("path", "to", "file")));
Jason
  • 5,154
  • 2
  • 12
  • 22
1

If your goal is to write to file eventually, you can also use jackson apis.

ObjectMapper mapper = new ObjectMapper();
//To add indentation to output json
mapper.enable(SerializationFeature.INDENT_OUTPUT);
Collection<ITestResult> results = iTestContext.getFailedTests().getAllResults();
try{
  mapper.writeValue(new File("/somepath/output.json"), results);
catch (IOException){
  e.printStackTrace();
}

Note: Recommended to use single instance of object mapper

Amstel D'Almeida
  • 630
  • 6
  • 13
1

For following snippet:

public static final class Node {
    class Employee {
        private final String employeeFirstName;
        private final String employeeLastName;
        private final String employeeDepartment;

        public Employee(String employeeFirstName, String employeeLastName, String employeeDepartment) {
            this.employeeFirstName = employeeFirstName;
            this.employeeLastName = employeeLastName;
            this.employeeDepartment = employeeDepartment;
        }
    }

    List<Employee> employees = Arrays.asList(
            new Employee("Mark", "Williams", "Sales"),
            new Employee("John", "Carpenter", "Accounts"),
            new Employee("David", "Hunter", "Marketing"));

    // String json = ...
}

Using gson-utils

String json = GsonUtils.writeValue(data);

Using jackson-utils

String json = JacksonUtils.writeValue(data);
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35