I have the following JSON reponse format. I would like to save only specific information on salesforce i.e. all the user details, description, zip code, location, request_status, space and state:

    "request_type_name":"Street Lighting",
    "department_name":"Customer Service Centre",
    "gov_user":"Sandy Burke",
    "user_detail":{"firstname":"Tony","lastname":"Andrew","email":"[email protected]","phone":"","address":null,"zipcode":null,"space":null,"state":null},
        "title":"Street Lighting",
        "description":"TEST API test",
        "zipcode":"PE1 1EJ",
        "location":"Peterborough, England",
        "url_title":"Publicstuff ~ Street Lighting ~ Street Lighting in Peterborough",
                "name":"What type of fault is it?","description":"",
                "value":"Light out",
                        "name":"What is the number displayed on the light column?",
                        "description":"Each street light has a unique number and we ask that you provide this in the above box.  The number format is 'PCC' followed by numbers for example, PCC12.  ",

I have tried the following: Creates wrapper classes:

public class DeserializedJSONresponse{

public class UserDetails
    public final Integer request_id;
    public final Integer request_type_id;
    public final String request_type_foreign_id;
    public final Integer request_type_cost;
    public final String request_type_name;
    public final Integer department_id;
    public final String department_name;
    public final String gov_user;
    public final Integer foreign_id;
    public final Boolean display_foreign_id;
    public final String device;
    public final String user;

    public final List<User_Details> user_detail;

    public final String request_status;
    public final Integer date_created;
    public final Integer date_due;
    public final Integer count_followers;
    public final Integer count_comments;
    public final Integer count_supporters;
    public final Integer count_flagged;
    public final String title;
    public final String description;
    public final Boolean is_visible;
    public final String address;
    public final String zipcode;
    public final String location;
    public final String space;
    public final String state;
    public final String priority;
    public final String category;
    public final Integer lat;
    public final Integer lon;
    public final Blob image_thumbnail;
    public final Blob image;
    public final String request_url;
    public final String url_title;
    public final List<CustomFields> custom_fields;
    public final List<statuses> status;
} .. other wrapper classes follows

and also a function to set up Http and Deserialize the JSON object:

public String postRequestCall(){
    JSONGenerator gen = JSON.createGenerator(true);

    HttpRequest req = new HttpRequest();
    req.setHeader('PublicStuff-Key', '5srT1Fdaeal25f23***********');
    Http http = new Http();
    HTTPResponse res = http.send(req);        

    String resonseBody = res.getBody();
    JSONParser parser = JSON.createParser(res.getBody());

    DeserializedJSONresponse reqs = new DeserializedJSONresponse();
    DeserializedJSONresponse.UserDetails URequest= (DeserializedJSONresponse.UserDetails)System.JSON.deserialize(resonseBody, DeserializedJSONresponse.UserDetails.class);
    System.debug('PublicStuffTrialClass UserRequest ' + URequest);

    return res.getBody();
  • 1
    What problem do you have with your code? (I notice that you should probably be passing responseBody.response into your deserialize.)
    – Keith C
    Nov 19, 2014 at 14:40
  • My URequests Values just returns null for all. Also, I cannot pass responseBody.response. that just returns an error.
    – codeSlayer
    Nov 19, 2014 at 14:47
  • Sorry about the responseBody.response comment - you are right that won't work as its text at that point. But as the incoming JSON starts with an object having a response field you will have to account for that in your parsing logic somewhere.
    – Keith C
    Nov 19, 2014 at 14:51
  • When you deserialize into an instance, that instance doesn't need to have all the fields the JSON includes. The deserializer will skip the fields that don't exist in the the destination class. Also, inside your wrapper class you can have a separate sobject field for the record you /do/ want to save--and only copy into it the values you need to.
    – tggagne
    Nov 25, 2014 at 3:56

1 Answer 1


I prefer using the JSON.deserializeUntyped() to get specific values.

//use the Map<String,Object> to get the values of the JSON
String jsonString = 'YOUR JSON BODY';
Map<String,Object> rawObj = (Map<String,Object>) JSON.deserializeUntyped(jsonString);
//the response fields are stored as an object in the "response": field. So you have to get that field before getting it's child fields.
Map<String,Object> responseObj = (Map<String,Object>)rawObj.get('response');
//this will get child objects
Map<String,Object> userDetails = (Map<String,Object>) responseObj.get('user_detail');
//loop through details
for (String field : userDetails.keySet()){
    system.debug('The field: ' + field);
    Object o = userDetails.get(ks);
    system.debug('The value: ' + o);
    //if you want to reference an sObject field you could do
//select a specific value
String requestStatus = (String) responseObj.get('request_status');
String firstName = (String) userDetails.get('firstname');

You could also use a List<Object> listObj = responseObj.get('arrayVar'); if the value is an array and then loop through that.

  • I tried your solution but in my debug log, I am getting an error of : System.NullPointerException: Attempt to de-reference a null object. Any help? thx
    – codeSlayer
    Nov 19, 2014 at 15:04
  • also, if i put a debug on the requestStatus and firstName, it returns null for both values
    – codeSlayer
    Nov 19, 2014 at 15:19
  • The two fields are null because the responseObj is erroring out. What field is getting the system error? can you validate you're getting the response body successfully?
    – BradByte
    Nov 19, 2014 at 15:31
  • I edited the code above to account for the fields being stored as an object called "response" in the JSON. Try it again and let me know if it works.
    – BradByte
    Nov 19, 2014 at 15:35
  • it works perfectly now!! as these user details will be dynamic/more than one. how can i pull lists of i.e. the user_detail?? thanks
    – codeSlayer
    Nov 19, 2014 at 15:54

You must log in to answer this question.

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