Disclaimer: I am new to JSON parsing in Salesforce, trying to up skill.

I am trying to extract the picture links, lets say the 'large' to be able to add to a contact record on record creation;

  "results": [
      "picture": {
        "large": "https://randomuser.me/api/portraits/women/9.jpg",
        "medium": "https://randomuser.me/api/portraits/med/women/9.jpg",
        "thumbnail": "https://randomuser.me/api/portraits/thumb/women/9.jpg"

I have made a REST callout (https://randomuser.me/api/?gender=female&inc=picture&noinfo) via the following;

Http http = new Http();
    HttpRequest request = new HttpRequest();
    HttpResponse response = http.send(request);
    // Sucessful, then parse response
    if (response.getStatusCode() == 200) {
        Map<String, Object> jsonResp = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
        List<Object> results = (List<Object>) jsonResp.get('results');
        for(Object res : results){
            Map<String, Object> resultMap = (Map<String, Object>) res;
            Map<String, Object> resultObj = (Map<String, Object>) resultMap.get('result');

The above is my latest attempt. I have also tried class variables (below) but in each approach I end up with null or 'doesnt exist' when trying to access the links.

public class Contact_JSON2Apex {
    public List<Results> results;

public class Results {
    public Picture picture;

public class Picture {
    public String large;

return (Contact_JSON2Apex) System.JSON.deserialize(json, Contact_JSON2Apex.class);

Which method would the best approach? And how would one then access the individual link value?

Thanks in advance

  • 2
    Please check this answer, it explains JSON deserializing in a greater detail: How do I get started working with JSON in Apex?
    – Raul
    Jul 6, 2021 at 11:27
  • 1
    There is no "result" property so this will return null get('result') .
    – Keith C
    Jul 6, 2021 at 11:44
  • Also you haven't defined res anywhere so it is null. Jul 6, 2021 at 13:43
  • Thank you all for your input.
    – John
    Jul 10, 2021 at 8:33

1 Answer 1


I think you made a pretty good first attempt. There's a few ways you can go about it. Personally, I like to use class deserialization due to its simplicity and type enforcement, unless there are things like reserved keywords to deal with. Here's my approach:

JSON Class

public without sharing class ResultsJSON {
   public List<ResultItem> results {get;}

    * Inner Classes
   public class ResultItem {
      public PictureItem picture {get;}

   public class PictureItem {
      public String large {get;}
      public String medium {get;}
      public String thumbnail {get;}

The "without sharing" within the class definition isn't required, but good practice to indicate that your class doesn't require the application of sharing rules if not accessing any records within your org.

Using the Class

ResultsJSON response = (ResultsJSON)JSON.deserialize(jsonString, ResultsJSON.class);
System.debug(response.results[0].picture.large); // <= This should print the link for the "large" image

Hope this helps!

  • Thank you very much - that has cleared it up for me.
    – John
    Jul 10, 2021 at 8:22

You must log in to answer this question.

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