0

I need to remove the extra bracket from the JSON Body which i am sending in a Request:

Please find the JSON in below:

    [
        {
            "securianDocNumbers":["3790772","3790771"],
            "caseId":"50018000009cGfEAAU"
        },
        {
            "securianDocNumbers":["3790771","3790771","529135254"],
            "caseId":"50018000009cHTsAAM"
        }
    ]

I just Want to remove the extra Square Bracket Outside in the JSON Format:

Below Code For JSON Generation

public static void frameCaseWebService(){

    //Query out the data from Salesforce database.
    List<Case> caseList = [Select id From Case Where Owner.Name ='WMG Ops - Follow Up' AND Has_Hit_Follow_Up__c = False AND 
                           Type = 'New Application' AND Sub_Type__c IN ('ML VA','ML EA','ML FA','Fee Based ML VA')];

    System.debug('caseList=='+caseList);

    Case_Associated_Document__c[] caseAssoDoc = [SELECT Id,Service_Request__r.Id, Document_Id__c FROM Case_Associated_Document__c WHERE Service_Request__r.Id = :caseList];

    Map<String,List<String>> mapForJson = new Map<String,List<String>>();

    for(Case_Associated_Document__c caseAssoDoc1 :caseAssoDoc){
        if(mapForJson.containsKey(caseAssoDoc1.Service_Request__r.Id))
            mapForJson.get(caseAssoDoc1.Service_Request__r.Id).add(caseAssoDoc1.Document_Id__c);
        else
            mapForJson.put(caseAssoDoc1.Service_Request__r.Id,new List<String>{caseAssoDoc1.Document_Id__c});
    }

    List<CaseDetails> lst = new List<CaseDetails>();
    for( String obj : mapForJson.keySet() ){
        lst.add(new CaseDetails(obj,mapForJson.get(obj)));
    }

    String jsonBody = json.serialize(lst);
    System.debug('jsonBody::'+jsonBody);
4
  • There is no extra bracket. I think you are running into X-Y Problem. Can you post exact requirement/issue ? Sep 18 '19 at 8:26
  • The Extra Square[ ] Bracket Outside of the JSON Starting From { "securianDocNumbers"is creating problem for my request. it is saying bad request
    – user74188
    Sep 18 '19 at 8:28
  • Wont that make JSON as invalid? Sep 18 '19 at 8:29
  • Sorry : I just Check I am getting the error : 14:06:19:439 USER_DEBUG [303]|DEBUG|responseBody=={"httpStatus":400,"httpRequestUri":"/SfsImagingServices/Documents/followup/annuity","message":"JSON parse error: Cannot deserialize instance of com.minnesotamutual.wmg.sfsimaging.client.services.beans.DocumentsFollowupRequest out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `com.minnesotamutual.wmg.sfsimaging.client.services.beans.DocumentsFollowup
    – user74188
    Sep 18 '19 at 8:37
0

The "extra bracket" reflects that you potentially don't just have a single record but could have many records - an array. If the receiving service only accepts one record at a time then you will have to make multiple send requests:

for( String obj : mapForJson.keySet() ){
    CaseDetails cd = new CaseDetails(obj,mapForJson.get(obj))
    String jsonBody = json.serialize(cd);
    // Send the JSON here
}
1
  • Thanks it Works
    – user74188
    Sep 18 '19 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy