I found contact data as JSON on the web that is not organized as the Contact sObject as the phone number is being stored as hierarchical data: please see https://api.androidhive.info/contacts/ for the structure.

id: "c200"
name:   "Ravi Tamada"
email:  "[email protected]"
address:    "xx-xx-xxxx,x - street, x - country"
gender: "male"
mobile: "+91 0000000000"
home:   "00 000000"
office: "00 000000"

This is the callout for the data:

public static HttpResponse makeCallout() {
    Http http = new Http();
    HttpRequest request = new HttpRequest();
    HttpResponse response = http.send(request);

    // If the request is successful, parse the JSON response.
    if (response.getStatusCode() == 200) {
        // Deserializes 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 'contacts' key as a list
        List<Object> contacts = (List<Object>) results.get('contacts');
        System.debug('Received the following contacts:');
        for (Object contact: contacts) {
    return response;

In the for loop I now can define the object Contact and assign values from the object. For the hierarchical data, would I write the following code:

Contact cont = new Contact();
cont.phone = contact.phone.home; ?

Thank you for your advice!

1 Answer 1


Create a List<Contact> and you can populate the equivalent fields one-by-one with the for loop.

You could see the phone structure and setup the Contact record like this:

List<Contact> sfContacts = new List<Contact>();
for (Object contact: contacts) {
    Contact newCont = new Contact();
    newCont.MobilePhone = contact.get('phone').get('mobile');
    // etc...

insert sfContacts;
  • Ok, thank you very much, that is the answer I needed! Apr 30, 2019 at 10:00
  • Hi I get an error "Method does not exist or incorrect signature: void get(String) from the type Object"! Apr 30, 2019 at 10:10
  • I just read that accessing properties in objects is not possible in Apex. Read salesforce.stackexchange.com/questions/135891/…. So there must be a different solution to the problem of accessing hierarchical data? Apr 30, 2019 at 11:30
  • Try another JSON.deserializeUntyped() on the contact.get('phone'). The whole structure you linked is all JSON, so you should be able to deserialize all the way down Apr 30, 2019 at 11:34
  • I think you can also just type case it to Map<String, Object>, like: newContact.MobilePhone = ((Map<String, Object>)contact.get('phone')).get('mobile'); Apr 30, 2019 at 12:33

You must log in to answer this question.

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