0

The code below is for fetching values from the request payload. However we are stuck where there is an inner element of type array called "doc" present under "smt". From this array element we need to fetch value of a particular element.

JSON Payload:

"smt": [
      {
        "smtNr": "01711VFLLH9",
        "smtOidNr": "72CCABF1FD294FAC990B1AB421678E0C",
        "busLinCd": "SML",
        "dcvDt": "2023-09-11",
        "smtCnxDscTe": "BONE CEMENT STANDERD VISCOSITY WITH GENTAMICINE CE",
        "bilTerTypCd": "P/P",
        "codIr": "0",
        "dclVluA": "340905.09",
        "dclVluCcyCd": "PHP",
        "frtChgNetA": "26078.26",
        "frtChgNetCcyCd": "PHP",
        "frtChgGrsA": "98100.88",
        "frtChgGrsCcyCd": "PHP",
        "fobIsnA": "0.00",
        "fobIsnCcyCd": "PHP",
        "frtDctA": "72022.62",
        "frtDctCcyCd": "PHP",
        "isnVluA": "0.00",
        "isnVluCcyCd": "PHP",
        "othChgA": "0.00",
        "othChgCcyCd": "PHP",
        "totLclVluA": "0.00",
        "wgtQy": "36.00",
        "wgtMsuMnmTe": "KGS",
        "netWgtQy": "0.00",
        "dwQy": "57.60",
        "dwMsuMnmTe": "KGS",
        "pkgQy": "1",
        "pkgMsuMnmTe": "BOX",
        "cnlSmtQy": "0.00",
        "achRetDt": "2030-09-14",
        "crtTs": "2023-09-11-17.21.14.876804",
        "actDptDt": "2023-09-11",
        "actArrDt": "2023-09-14",
        "prgDt": "2024-09-14",
        "smtDt": "2023-09-11",
        "sysCrtDt": "2023-09-11",
        "bilTypCd": "03",
        "bkrAgyNa": "UPS",
        "bkrAgyCd": "237",
        "bkrAgyCnyCd": "PH",
        "bkrRefNr": "A58D9294F35D44D2958013E8E580BE19",
        "bypConNr": "0",
        "cogTypCd": "X",
        "sdlIr": "0",
        "ctmNspReqIr": "0",
        "dtyIr": "0",
        "dtyTaxCrIr": "0",
        "xptDocIclIr": "0",
        "hzmArcIr": "0",
        "icpSmtIr": "0",
        "mvmCvyIdNr": "5X0138",
        "npIr": "1",
        "dtyTypCd": "01",
        "rltPtyIr": "0",
        "rtnSmtIr": "0",
        "cnlClnIr": "0",
        "ctmHldIr": "0",
        "svcTypCd": "05",
        "smtSplIr": "0",
        "smtTypCd": "01",
        "snfChnIr": "1",
        "tshSndIr": "0",
        "prcCnyCd": "PH",
        "smtUidNr": "A58D9294F35D44D2958013E8E580BE19",
        
        
        "doc": [
          {
            "docTypCd": "MAWB",
            "docDscTe": "Master Air Waybill NumbLer",
            "docNr": "40678700156"
          },
          {
            "docTypCd": "MWBOID",
            "docDscTe": "Master Waybill OID",
            "docNr": "41D46D678BC64A569CB8990E8CA59A36"
          }
        ]
]

REST API Code:

@RestResource(urlMapping='/api/Shipment/*')
global with sharing class PBP_ShipmentRestAPI
{
    private static DeserializeJson desJson;
    
    @HttpPost
    global static void createShipment() {
        
        desJson = (DeserializeJson)JSON.deserialize(RestContext.request.requestBody.toString(),DeserializeJson.class);
        if(desJson!=null){
            if(desJson.smt!=null){
                System.debug('DesJson>>'+desJson.smt);
                processCreateShipment();
            } 
        }
    }
    
    public static void processCreateShipment(){
        System.debug('DesJson>>'+desJson.smt);
        PBP_Shipment__c shipment = new PBP_Shipment__c();
        if(desJson.smt[0].smtOidNr!=null){
            shipment.PBP_Shipment_Object_Id__c = desJson.smt[0].smtOidNr;//(External ID) (Unique Case Insensitive)
        }
        if(desJson.smt[0].prcCnyCd!=null){
            shipment.PBP_Brokerage_Processing_Country__c = desJson.smt[0].prcCnyCd;
        }
        if(desJson.smt[0].mvmCvyIdNr!=null){
            shipment.PBP_Movement_Number__c = desJson.smt[0].mvmCvyIdNr;
        }
        if(desJson.smt[0].arcFltNr!=null){
            shipment.PBP_Flight_Voyage_Number__c = desJson.smt[0].arcFltNr;
        }
        if(desJson.smt[0].arcFltNr!=null){
            shipment.PBP_Flight_Voyage_Number__c = desJson.smt[0].arcFltNr;
        }
        if(desJson.smt[0].actArrDt!=null){
            shipment.PBP_Flight_Arrival_Date__c = desJson.smt[0].actArrDt;
        }
        if(desJson.smt[0].svcTypCd!=null){
            shipment.PBP_Service_Level__c = desJson.smt[0].svcTypCd;
        }
        if(desJson.smt[0].dclVluA!=null){
            shipment.PBP_Shipment_Value__c = desJson.smt[0].dclVluA;
        }
        if(desJson.smt[0].dclVluCcyCd!=null){
            shipment.PBP_Shipment_Value_Currency__c = desJson.smt[0].dclVluCcyCd;
        }
        if(desJson.smt[0].bilTerTypCd!=null){
            shipment.PBP_Bill_Term__c = desJson.smt[0].bilTerTypCd;
        }
        if(desJson.smt[0].cogNr!=null){
            shipment.PBP_Certificate_of_Origin__c = desJson.smt[0].cogNr;
        }
        if(desJson.smt[0].codIr!=null){
            shipment.PBP_COD__c = Boolean.valueOf(desJson.smt[0].codIr);
        }
        if(desJson.smt[0].smtSplIr!=null){
            shipment.PBP_Split_Shipment__c = Boolean.valueOf(desJson.smt[0].smtSplIr);
        }
        if(desJson.smt[0].pkgQy!=null){
            shipment.PBP_No_Of_Packages__c = desJson.smt[0].pkgQy;
        }
        if(desJson.smt[0].smtNr!=null){
            shipment.PBP_Shipment_Number_ID__c = desJson.smt[0].smtNr;
        }
        if(desJson.smt[0].doc!=null){
            if(desJson.smt[0].doc.size()>1){
                
            }
        }
        
        upsert shipment PBP_Shipment_Object_Id__c;
        
    }
    
    public class DeserializeJson{
        public List<Smt> smt {get;set;}
    }
    
    public class Smt {
        public String smtOidNr  {get;set;}
        public String prcCnyCd  {get;set;}
        public String tsmCnyCd  {get;set;}
        public String mvmCvyIdNr    {get;set;}
        public String arcFltNr  {get;set;}
        public String actArrDt  {get;set;}
        public String svcTypCd  {get;set;}
        public String dclVluA   {get;set;}
        public String dclVluCcyCd   {get;set;}
        public String bilTerTypCd   {get;set;}
        public String cogNr {get;set;}
        public String codIr {get;set;}
        public String smtSplIr  {get;set;}
        public Decimal pkgQy    {get;set;}
        public String smtNr {get;set;}
        public List<doc> doc {get;set;}
    }
    
    public class doc {
        public String docNr {get;set;}
        public String docTypCd  {get;set;}
        public String docDscTe  {get;set;}
    }

}
5
  • 5
    did you consider the usage of JSON2Apex ? Hope it is still working. Commented Dec 12, 2023 at 11:40
  • Tried but its not working as expected...As per the JSON payload given we need to be able to fetch element of "doc" which is an array and comes under "smt" element. I used this same payload in JSON2Apex but the code generated did not serve the purpose. Commented Dec 12, 2023 at 12:44
  • Quick action or help on this will really be appreciated. Thanks in advance. Commented Dec 12, 2023 at 12:45
  • 2
    Honestly, this is not a very complex JSON payload. As the answer says, make sure you have a valid payload before trying to use JSON2Apex. Note that VSCode (and other IDEs) can help you with that since it supports both plain JSON editing but also can provide schema aware editing (when you have a JSON schema available).
    – Phil W
    Commented Dec 12, 2023 at 13:40
  • 1
    @OleksandrBerehovskyi I'm working on a new version, but the current version still works. :D
    – sfdcfox
    Commented Dec 12, 2023 at 15:25

1 Answer 1

3

The JSON you are using is malformed try validating it online. Correct JSON will be -

{
  "smt": [
    {
      "smtNr": "01711VFLLH9",
      "smtOidNr": "72CCABF1FD294FAC990B1AB421678E0C",
      "busLinCd": "SML",
      "dcvDt": "2023-09-11",
      "smtCnxDscTe": "BONE CEMENT STANDERD VISCOSITY WITH GENTAMICINE CE",
      "bilTerTypCd": "P/P",
      "codIr": "0",
      "dclVluA": "340905.09",
      "dclVluCcyCd": "PHP",
      "frtChgNetA": "26078.26",
      "frtChgNetCcyCd": "PHP",
      "frtChgGrsA": "98100.88",
      "frtChgGrsCcyCd": "PHP",
      "fobIsnA": "0.00",
      "fobIsnCcyCd": "PHP",
      "frtDctA": "72022.62",
      "frtDctCcyCd": "PHP",
      "isnVluA": "0.00",
      "isnVluCcyCd": "PHP",
      "othChgA": "0.00",
      "othChgCcyCd": "PHP",
      "totLclVluA": "0.00",
      "wgtQy": "36.00",
      "wgtMsuMnmTe": "KGS",
      "netWgtQy": "0.00",
      "dwQy": "57.60",
      "dwMsuMnmTe": "KGS",
      "pkgQy": "1",
      "pkgMsuMnmTe": "BOX",
      "cnlSmtQy": "0.00",
      "achRetDt": "2030-09-14",
      "crtTs": "2023-09-11-17.21.14.876804",
      "actDptDt": "2023-09-11",
      "actArrDt": "2023-09-14",
      "prgDt": "2024-09-14",
      "smtDt": "2023-09-11",
      "sysCrtDt": "2023-09-11",
      "bilTypCd": "03",
      "bkrAgyNa": "UPS",
      "bkrAgyCd": "237",
      "bkrAgyCnyCd": "PH",
      "bkrRefNr": "A58D9294F35D44D2958013E8E580BE19",
      "bypConNr": "0",
      "cogTypCd": "X",
      "sdlIr": "0",
      "ctmNspReqIr": "0",
      "dtyIr": "0",
      "dtyTaxCrIr": "0",
      "xptDocIclIr": "0",
      "hzmArcIr": "0",
      "icpSmtIr": "0",
      "mvmCvyIdNr": "5X0138",
      "npIr": "1",
      "dtyTypCd": "01",
      "rltPtyIr": "0",
      "rtnSmtIr": "0",
      "cnlClnIr": "0",
      "ctmHldIr": "0",
      "svcTypCd": "05",
      "smtSplIr": "0",
      "smtTypCd": "01",
      "snfChnIr": "1",
      "tshSndIr": "0",
      "prcCnyCd": "PH",
      "smtUidNr": "A58D9294F35D44D2958013E8E580BE19",
      "doc": [
        {
          "docTypCd": "MAWB",
          "docDscTe": "Master Air Waybill NumbLer",
          "docNr": "40678700156"
        },
        {
          "docTypCd": "MWBOID",
          "docDscTe": "Master Waybill OID",
          "docNr": "41D46D678BC64A569CB8990E8CA59A36"
        }
      ]
    }
  ]
}

Use this JSON on https://json2apex.herokuapp.com/, it will surely generate you a class. I tried and it works for me. Class -

//
// Generated by JSON2Apex http://json2apex.herokuapp.com/
//

public class JSON2Apex {

    public List<Smt> smt;

    public class Doc {
        public String docTypCd;
        public String docDscTe;
        public String docNr;
    }

    public class Smt {
        public String smtNr;
        public String smtOidNr;
        public String busLinCd;
        public String dcvDt;
        public String smtCnxDscTe;
        public String bilTerTypCd;
        public String codIr;
        public String dclVluA;
        public String dclVluCcyCd;
        public String frtChgNetA;
        public String frtChgNetCcyCd;
        public String frtChgGrsA;
        public String frtChgGrsCcyCd;
        public String fobIsnA;
        public String fobIsnCcyCd;
        public String frtDctA;
        public String frtDctCcyCd;
        public String isnVluA;
        public String isnVluCcyCd;
        public String othChgA;
        public String othChgCcyCd;
        public String totLclVluA;
        public String wgtQy;
        public String wgtMsuMnmTe;
        public String netWgtQy;
        public String dwQy;
        public String dwMsuMnmTe;
        public String pkgQy;
        public String pkgMsuMnmTe;
        public String cnlSmtQy;
        public String achRetDt;
        public String crtTs;
        public String actDptDt;
        public String actArrDt;
        public String prgDt;
        public String smtDt;
        public String sysCrtDt;
        public String bilTypCd;
        public String bkrAgyNa;
        public String bkrAgyCd;
        public String bkrAgyCnyCd;
        public String bkrRefNr;
        public String bypConNr;
        public String cogTypCd;
        public String sdlIr;
        public String ctmNspReqIr;
        public String dtyIr;
        public String dtyTaxCrIr;
        public String xptDocIclIr;
        public String hzmArcIr;
        public String icpSmtIr;
        public String mvmCvyIdNr;
        public String npIr;
        public String dtyTypCd;
        public String rltPtyIr;
        public String rtnSmtIr;
        public String cnlClnIr;
        public String ctmHldIr;
        public String svcTypCd;
        public String smtSplIr;
        public String smtTypCd;
        public String snfChnIr;
        public String tshSndIr;
        public String prcCnyCd;
        public String smtUidNr;
        public List<Doc> doc;
    }

    
    public static JSON2Apex parse(String json) {
        return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
    }
}

To get the Doc -

        JSON2Apex parsed = JSON2Apex.parse(complexJSON);
        if(parsed!=null){
            if(parsed.smt!=null){
                System.debug('complexJSON>>'+parsed.smt[0].doc);
            } 
        }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .