-1

I have this test scenario where I need to

  • fetch details from an API,
  • write it to a file ,
  • make updates to a JSON array and update the file
  • use the new file as a payload.

I've been able to do the first two steps but not able proceed in the further steps, as I haven't played with JSON in Java.

API result from Step 1 :

{"id":123,"type":"employee","employee_id":"5162231","full_name":"Mark Hamil","email_id":"mark_ham@ham.test","created_at":"2014-03-26","updated_at":"2015-04-06","empno":9122,"branches":[{"name":"Assets","asset_id":"56","attr_typ":"Assets","asset_path":"12-34-56789","is_pr":false,"is_lead":false},{"name":"Retail Banking","asset_id":"8","attr_typ":"RB","asset_path":"8-12-11221","is_pr":false,"is_lead":true},{"name":"Corporate Banking","asset_id":"94","attr_typ":"CB","asset_path":"94-122-22123","is_pr":false,"is_lead":true},{"name":"Rural Banking","asset_id":"118","attr_typ":"RuB","asset_path":"97-1188","is_pr":false,"is_lead":true}],"email_freq":{"id":21,"immediately":"N","daily":"N","weekly":"N","monthly":"N","ind_id":1136},"responsible_for":[{"region":"AS","office_code":"TH","office_loc":"BNK","name":"Asia - Thailand - Bangkok"}]}

Updates to make :

Need to add a new json object for responsible office in responsible_for key. Updated value for responsible_key need to look like

"responsible_for":[{"region":"AS","office_code":"TH","office_loc":"BNK","name":"Asia - Thailand - Bangkok"},{"region":"AS","office_code":"ML","office_loc":"KLP","name":"Asia - Malaysia - Kualalumpur"}]}

Code so far

I have made a method which performs all the four step in once -

     public static void change_person_details_json()throws IOException {

     // get the person details after appending the id with base path/person          

 HelperRest.hitGetRequest(ConfigReader.getBaseUrl()+"person/"+HelperRest.get_person_id());
                 System.out.println(HelperRest.getResponse().body().asString());
                 HelperRest.logEverything();

    //write the API response to a json file
                 try(FileWriter writer = new FileWriter(ConfigReader.getRequestPayloadFilePath()+"update-json.json")){
                     writer.write(HelperRest.getResponse().body().asString());
                     writer.flush();
                 } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
demouser123
  • 4,108
  • 9
  • 50
  • 82

1 Answers1

1

I can help you with the 3rd point. Adding the element to JSON.

I used org.json library

        <!-- https://mvnrepository.com/artifact/org.json/json -->
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20180813</version>
        </dependency>

You save the response to a .json file, but you don't have to do that. You can operate on JSON as a String with the given library. So instead of doing it this way:

//write the API response to a json file
                 try(FileWriter writer = new FileWriter(ConfigReader.getRequestPayloadFilePath()+"update-json.json")){
                     writer.write(HelperRest.getResponse().body().asString());
                     writer.flush();
                 } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

You can use

String jsonResponse = HelperRest.getResponse().body().asString();

JSONObject root = new JSONObject(jsonResponse);
JSONArray responsibleForArray = root.getJSONArray("responsible_for");

HashMap<String, String> newResponsibleForMap = new HashMap<>();
newResponsibleForMap.put("region", "AS");
newResponsibleForMap.put("office_code", "ML");
newResponsibleForMap.put("office_loc", "KLP");
newResponsibleForMap.put("name", "Asia - Malaysia - Kualalumpur");

responsibleForArray.put(newResponsibleForMap);

System.out.println(root);

Hope it helps in some way!

Fenio
  • 3,528
  • 1
  • 13
  • 27