Reputation: 697
I have json file Called "addplace.json" whose contents are pasted below. Now I need to read above said json file and change the values for Latitude, longitutde ,name,phone,Address. So how would i do that , please advise me with sample java code.
{
"location": {
"lat": -33.8669710,
"lng": 151.1958750
},
"accuracy": 50,
"name": "Google Shoes!",
"phone_number": "(02) 9374 4000",
"address": "48 Pirrama Road, Pyrmont, NSW 2009, Australia",
"types": ["shoe_store"],
"website": "http://www.google.com.au/",
"language": "en-AU"
}
Thanks in Advance.
Upvotes: 2
Views: 1806
Reputation: 697
Sorry forgot delayed response.Below is the code that i used to read the json file at runtime , modify it and fire the modified json as part of post operation
public class Post_Ex3 extends BaseStepClass{
public Scenario scenario;
@Before
public void beforeTest(Scenario scenario)
{
this.scenario =scenario;
}
@When("user changes the attributes values like name {string} , Salary {string} and age {string} in json payload file {string}")
public void user_changes_the_attributes_values_like_name_Salary_and_age_in_json_payload_file(String name, String salary, String age, String payloadfileName) {
String filepath = System.getProperty("user.dir")+"\\src\\test\\resources\\JsonFiles\\"+payloadfileName;
System.out.println("path : " +filepath);
try {
String jsonContents = new String((Files.readAllBytes(Paths.get(filepath))));
JSONObject jsonObject= new JSONObject(jsonContents);
System.out.println(" Post Payload Before changes: ");
System.out.println("============================");
System.out.println(jsonObject.toString(4));
//changing the value of name from tammy to balaji
jsonObject.put("name", name);
jsonObject.put("salary", salary);
jsonObject.put("age", age);
System.out.println(" Post Payload after changes: ");
System.out.println("============================");
System.out.println(jsonObject.toString(4));
String payload = jsonObject.toString(4);
scenario.write("Payload Passed along with post request");
scenario.write(payload);
// Add a header stating the Request body is a JSON
httpRequest.header("Content-Type", "application/json");
// Add the Json to the body of the request
httpRequest.body(jsonObject.toString(4));
} catch (IOException e) {
System.out.println("No file found in the path ");
e.printStackTrace();
}
}
@When("user initiates post request with {string} endpoint")
public void user_initiates_post_request_with_endpoint(String string) {
response = httpRequest.post("/create");
}
@Then("user should get an status code as {string} in post response")
public void user_should_get_an_status_code_as_in_post_response(String expectedStatusCode) {
int statusCode = response.getStatusCode();
Assert.assertEquals(statusCode, Integer.parseInt(expectedStatusCode),"Failed due to Server issue -"+statusCode);
Assert.assertNotNull(response.asString());
System.out.println("Response :" + response.prettyPrint());
System.out.println("Status Code :" + response.getStatusCode());
System.out.println("Does Reponse contains 'tammy'? :" + response.asString().contains("tammy"));
String name = response.jsonPath().get("data.name").toString();
System.out.println(" name : " +name);
System.out.println(" Salary value in response : " +response.jsonPath().get("data.salary").toString());
}
}
Upvotes: 1