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
Janet Putnam 2Janet Putnam 2 

deserialize untyped json

I am trying to display json data in a visual force page. In the process of that I'm using Execute Anonymous to pinpoint my errors. I have code sample from a trail that works:
 
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals');
request.setMethod('GET');
HttpResponse response = http.send(request);
// If the request is successful, parse the JSON response.
if (response.getStatusCode() == 200) {
    // Deserialize the JSON string into collections of primitive data types.
    Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
    // Cast the values in the 'animals' key as a list
    List<Object> animals = (List<Object>) results.get('animals');
    System.debug('Received the following animals:');
    for (Object animal: animals) {
        System.debug(animal);
    }
}

The above url produces json that looks like this:
{"animals":["majestic badger","fluffy bunny","scary bear","chicken"]}

So, my code is here:
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('http://12.15..13/GPDataAPI/api/values');
request.setMethod('GET');
HttpResponse response = http.send(request);
system.debug('response: ' + response.getBody());
// If the request is successful, parse the JSON response.
if (response.getStatusCode() == 200) {
    // Deserialize the JSON string into collections of primitive data types.
    Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
   // Cast the values in the 'animals' key as a list
   // This is where my issue is because my array doesn't have a 'named' element
    List<Object> payments = (List<Object>) results.get('payments');
    System.debug('Received the following animals:');
    for (Object payment: payments) {
        System.debug(payment);
    }
}

If I highlight and run only to the system.debug the code works and I can see in the logs that the body is correct. 
The source json for my call is:
[{"CustNmbr":"F592611 ","DocNmbr":"EBM01006295 ","DocDate":"2017-11-21T00:00:00","CheckNmbr":" ","Amt":100.00000,"Paid":"Paid","Posted":"2017-11-22T00:00:00","UnPaid":"100.00000"}]

I believe my problem is here:
List<Object> payments = (List<Object>) results.get('payments');
    System.debug('Received the following animals:');
    for (Object payment: payments) {
        System.debug(payment);
    }
and is because my source json is just an array of an object without a name (payments) where the SF code that does work has a key ('animals'). 

Any suggestions would be greatly appreciated.
 
Arun MKArun MK
hi,

Since your source json does not have the key name (payments), you are facing the issue. You can try the following inorder to retrieve the values.
List<Object> payments = (List<Object>) JSON.deserializeUntyped(response.getBody());
for (Object obj : payments) {
    Map<String, Object> payment = (Map<String, Object>)obj;
    System.debug(payment.get('CustNmbr'));
    System.debug(payment.get('DocNmbr'));
    System.debug(payment.get('DocDate'));
    System.debug(payment.get('CheckNmbr'));
    System.debug(payment.get('Paid'));
    System.debug(payment.get('Posted'));
    System.debug(payment.get('UnPaid'));
}

Let me know if it works fine and please mark it as solved if my reply was helpful.