1 - Read data from JSON
Google has a very good Java library to parse JSON in a structured way:
GSON
The types you would use the most are JSONObject and JSONElement.
2 - Use the data in a test
I would suggest having in your dataprovide only the name of the JSON file. In the test, you can call a factory that would read the JSON file and generate a POJO object.
E.g.,
@Test(dataprovider='JSON File Data')
public .... myTest(String jsonFileName) {
TestDataModel testData = TestDataFactory.createTestDataFromJSON(jsonFileName);
fillUserName(testData.getUserName());
fillUserName(testData.getPassword());
...
ExcelLogger.logTestDataInExcel(testData);
}
TestDataModel will represent the JSON file:
JSON:
{
"userName": "Mario",
"password": "Luigi"
}
POJO with a bit of Lombok:
public class TestDataModel {
@Getter @Setter
public final String userName;
@Getter @Setter
public final String password;
}
TestDataFactory and ExcelLogger will do the dirty work of interacting with the file system and performing the serialization/deserialization part.