I need to change application_id
every-time while running the script! Thanks in advance to explain in java!
My json file below:-
{
"APPLICATION": [{
"application_id": "4884850",
"appl_purpose_code": "LN",
"original_purpose": "LN",
"appl_status_code": "S"
}],
"AATCL_MAIN": [{
"application_id": "4884850",
"other_wireless_ind": "N",
"seek_rural_bc": "N"
}],
"A_LICENSE": [{
"application_id": "4884850",
"a_alien_officer": "N",
"a_alien_control": "N"
}]
}
My java code below:-
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import org.json.JSONObject;
import org.testng.annotations.Test;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class testing {
@Test
public void replaceText() throws JsonParseException, JsonMappingException, IOException {
ObjectMapper mapper = new ObjectMapper();
String key = "key"; //whatever
//("{key1:\"val1\", key2:\"val2\"}")
JSONObject jo = new JSONObject("{APPLICATION[0].application_id:\"4884852\"}");
//Read from file
JSONObject root = mapper.readValue(new File("jsonFileInputPost\\jsonGrouponePostFullContent.json"), JSONObject.class);
String val_newer = jo.getString(key);
String val_older = root.getString(key);
//Compare values
if(!val_newer.equals(val_older))
{
//Update value in object
root.put(key,val_newer);
//Write into the file
try (FileWriter file = new FileWriter("jsonFileInputPost\\jsonGrouponePostFullContent.json"))
{
file.write(root.toString());
System.out.println("Successfully updated json object to file...!!");
}
}
}
}