-1

I am trying to parse JSON into a string format in apex.The JSON format is

{
    "GenerationTime": "20200119170533",
    "ResponseCode": "MSK",
    "ValidationExceptions": [{
            "SettlementDate": "20200113",
            "MSL": "_A",
            "MSN": {
                "ImportMSID": "MSID",
                "ExportMSID": null
            },
            "MRA": {
                "SettlementPeriod": 34,
                "DeliveredVolume": 23,
                "ExceptionReason": "The import volume doesnot match with the actual allocation"
            }
        }, {
            "SettlementDate": "20200114",
            "MSL": "_B",
            "MSN": {
                "ImportMSID": "MSID",
                "ExportMSID": null
            },
            "MRA": {
                "SettlementPeriod": 45,
                "DeliveredVolume": 21,
                "ExceptionReason": "The import volume doesnot match with the actual allocation"
            }
        }
    ]
}

wrapper class for this.

public class ExcFileWrapper{
    Public String GenerationTime; 
    Public String ResponseCode;
    Public List <ValidationExcepWrapper> ValidationExceptions;
}

Public class ValidationExcepWrapper{      
    Public String SettlementDate; 
    Public String MSL; 
    Public MSNWrapper MSN;
    Public MRAWrapper MRA;
}
Public class MSNWrapper{
    Public String ImportMSID;
    Public String ExportMSID; 
}
Public class MRAWrapper{
   Public integer SettlementPeriod;
   Public integer DeliveredVolume;
   Public String ExceptionReason;
}

But I am getting error as "No content to map to Object due to end of input". Am i wrong in writing the wrapper class? Putting the classes "MSN Wrapper" and "MRA Wrapper" inside "ValidationExcepWrapper" is giving error as "Inner types are not allowed to have inner types"

i am trying to convert json to base64 and then to string

 base64Data = EncodingUtil.urlDecode(base64Data, 'UTF-8');        
 Blob afterblob = EncodingUtil.base64Decode(base64Data);
 String jsonData =  afterblob.toString(); //json string

after converting to string i am trying to parse

JSONParser parser = JSON.createParser(jsonData);

ExcFileWrapper rejWrpRec = (ExcFileWrapper)parser.readValueAs(ExcFileWrapper.class);

In the end all i want is to collect all the "SettlementDate" value in a list

  • 2
    Is that the actual string data you are passing to JSON.deserialize()? How exactly are you performing the deserialization? Please edit to clarify. – David Reed Apr 12 at 4:15
  • 3
    Does this answer your question? How do I get started working with JSON in Apex? – identigral Apr 12 at 4:23
  • @identigral i tried referring the link you provided but i still am not able to resolve my issue. – Return0 Apr 12 at 4:30
  • @DavidReed edited my answer to accomodate details – Return0 Apr 12 at 4:31
2

Your schema is fine. Your code, however, does not really make sense.

 base64Data = EncodingUtil.urlDecode(base64Data, 'UTF-8');        
 Blob afterblob = EncodingUtil.base64Decode(base64Data);
 String jsonData =  afterblob.toString(); //json string

Why are you doing this? Are you actually receiving a JSON payload in application/x-www-form-urlencoded format? Or in base64? It certainly should not be both, and I'd be rather surprised if it is either. Here, you're first decoding URL-encoded data, and then treating that data as base64, and then attempting to parse as JSON. It seems extremely unlikely to me that that is actually what you are receiving.

Most likely, you are just receiving a JSON string, and these attempted transformations are destroying it.

after converting to string i am trying to parse

JSONParser parser = JSON.createParser(jsonData);
AcceptFileWrapper wrpRec = (AcceptFileWrapper)parser.readValueAs(AcceptFileWrapper.class);

You should simply be using

AcceptFileWrapper wrpRec = (AcceptFileWrapper)JSON.deserialize(jsonData, AcceptFileWrapper.class);

There is no need to touch the JSONParser class.

| improve this answer | |

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.