0

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...!!");
        }
    }
}

}
Mahesh Waghmare
  • 726
  • 9
  • 27
Azad
  • 1
  • 1
  • https://stackoverflow.com/questions/28607255/how-can-i-replace-a-java-jackson-textnode-by-another-one-update/28649524#28649524 contains the answer, I believe. – 0xadecimal Oct 04 '19 at 16:10
  • Seems odd that you're using both Jackson and org.json.JSONObject. I'd recommend using Jackson's tree model instead of org.json, and reading the json using [ObjectMapper.readTree(File)](https://fasterxml.github.io/jackson-databind/javadoc/2.7/com/fasterxml/jackson/databind/ObjectMapper.html#readTree\(java.io.File\)). Then you can navigate the tree and replace the fields you want to update. – dnault Oct 04 '19 at 19:44

1 Answers1

0

I assume that the value of application_id belongs to APPLICATION, AATCL_MAIN and A_LICENSE is identical, then you can judge the given JSON string whether will be updated or not as follows:

String jsonStr = "{\"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\"}]}";

String applicationIdNew = "4884852";
ObjectMapper mapper = new ObjectMapper();
String applicationId = mapper.readTree(jsonStr).get("APPLICATION").get(0).get("application_id").asText();

ObjectNode root = (ObjectNode) mapper.readTree(jsonStr);
if (!applicationIdNew.equals(applicationId)) {
    ((ObjectNode) root.get("APPLICATION").get(0)).put("application_id", applicationIdNew);
    ((ObjectNode) root.get("AATCL_MAIN").get(0)).put("application_id", applicationIdNew);
    ((ObjectNode) root.get("A_LICENSE").get(0)).put("application_id", applicationIdNew);
}
System.out.println(root.toString());
LHCHIN
  • 3,679
  • 2
  • 16
  • 34
  • Thank you very much indeed for your help LHCHIN! Yes, this could be one way we can solve this problem!But, I already have a huge json file, so I was thinking, if possible to read the whole file and update "application_id" filed only! Originally I have to update 13 places! Please let me know, if you have better idea! Thanks again! – Azad Oct 08 '19 at 00:33
  • @Azad If you want to update "application_id" filed only, I think the most direct way is to use `String.replace()` such as `jsonStr.replace("\"application_id\": \"4884850\"", "\"application_id\": \"4884852\"");` while reading the file line by line. – LHCHIN Oct 08 '19 at 00:40