function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Alex Skempris 18Alex Skempris 18 

Access values of incoming JSON string.

Hi all,

I'm trying to convert an incoming response from api callout which comes in the form of a json string.

Response: 

[{"OriginalAccountNumber":"559643597","OriginalSortCode":"9043253","IsCorrect":true,"IsDirectDebitCapable":true,"StatusInformation":"OK","CorrectedSortCode":"9043253","CorrectedAccountNumber":"559643597","IBAN":"GB27NWBK9043253559643597","Bank":"TESTNATIONALBANK PLC","BankBIC":"LDBRTGR56","Branch":"Lincoln Smiths Bank","BranchBIC":"65Y","ContactAddressLine1":"Smiths Road Customer Service Centre","ContactAddressLine2":"Marina, Waterside Court","ContactPostTown":"York","ContactPostcode":"Y01 6FA","ContactPhone":"0870 2403355","ContactFax":""}]

so far I've tried 
            Map<String, Object> Results = (Map<String, Object>) JSON.deserializeUntyped(response);

But I get an error of illegal conversion from string to map. 

I've tried a few other methods including JSON.createParser but so far nothings has worked for me to access the values in an easy and efficient manner.

Can someone recommend a solution please?
Best Answer chosen by Alex Skempris 18
Alex Skempris 18Alex Skempris 18
Hi All,

Thank you all for your responses. Turns out I was being daft and was calling the API to be returned in a CSV form instead of JSON. As soon as I called the right API everything worked instantly. 

Thanks a lot for your help.

All Answers

Alex Skempris 18Alex Skempris 18
Hi Malika,

Thanks for your reply, still the same I'm afraid, error: 
System.TypeException: Invalid conversion from runtime type String to Map<String,ANY>

I've also tried putting them on a list wit intent of iterating through them like so: 

List<Object> lstResponse = (List<Object>) JSON.deserializeUntyped(response.getBody());

For the record I'm calling this through a static methog on my anonymous console.

Thanks
Alex
Prashant sharma 218Prashant sharma 218
Hi Alex,

We can create wrapper class for that please check below code :

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

public class TestWrapper {

    public String OriginalAccountNumber;
    public String OriginalSortCode;
    public Boolean IsCorrect;
    public Boolean IsDirectDebitCapable;
    public String StatusInformation;
    public String CorrectedSortCode;
    public String CorrectedAccountNumber;
    public String IBAN;
    public String Bank;
    public String BankBIC;
    public String Branch;
    public String BranchBIC;
    public String ContactAddressLine1;
    public String ContactAddressLine2;
    public String ContactPostTown;
    public String ContactPostcode;
    public String ContactPhone;
    public String ContactFax;

    
    public static List<TestWrapper> parse(String json) {
        return (List<TestWrapper>) System.JSON.deserialize(json, List<TestWrapper>.class);
    }
}

Accoes the values :

String json = '[{\"OriginalAccountNumber\":\"559643597\",\"OriginalSortCode\":\"9043253\",\"IsCorrect\":true,\"IsDirectDebitCapable\":true,\"StatusInformation\":\"OK\",\"CorrectedSortCode\":\"9043253\",\"CorrectedAccountNumber\":\"559643597\",\"IBAN\":\"GB27NWBK9043253559643597\",\"Bank\":\"TESTNATIONALBANK PLC\",\"BankBIC\":\"LDBRTGR56\",\"Branch\":\"Lincoln Smiths Bank\",\"BranchBIC\":\"65Y\",\"ContactAddressLine1\":\"Smiths Road Customer Service Centre\",\"ContactAddressLine2\":\"Marina, Waterside Court\",\"ContactPostTown\":\"York\",\"ContactPostcode\":\"Y01 6FA\",\"ContactPhone\":\"0870 2403355\",\"ContactFax\":\"\"}]';
        List<TestWrapper> obj = TestWrapper.parse(json);

TestWrapper testwrapp = obj[0];
system.debug('bank is >>>>>>. ' + testwrapp.Bank);

Please let em know if it helps.
SwethaSwetha (Salesforce Developers) 
HI Alex,
I believe you are converting from JSON to String. Does the approach in https://salesforce.stackexchange.com/questions/114319/convert-json-to-string work for you
Thanks
Alex Skempris 18Alex Skempris 18
Hi All,

Thank you all for your responses. Turns out I was being daft and was calling the API to be returned in a CSV form instead of JSON. As soon as I called the right API everything worked instantly. 

Thanks a lot for your help.
This was selected as the best answer