0

My webservice contains hierarchical data so that I cannot transform it into an sObject when making the GET request. Here is an example for Contact, but the phone number is grouped so that I cannot simply transform the data into the sObject Contact: https://api.androidhive.info/contacts/

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

Here is the transformation of JSON into an object:

    public static HttpResponse makeCallout() {
    Http http = new Http();
    HttpRequest request = new HttpRequest();
    request.setEndpoint('https://api.androidhive.info/contacts/');
    request.setMethod('GET');
    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) {
            System.debug(contact);
        }
    }
    return response;
}

Now I want to create a list of Contacts out of the result: list <Contact>.

This does not seem to function as it seems not to be possible for objects (not sObjects!) to access properties (Can we access class properties dynamically with Object class?):

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

insert sfContacts;

How do I generally convert hierarchical JSON data into a list of sObjects where the structure is not entirely the same?

2

1 Answer 1

3

One way to approach this is to define custom Apex types to match the structure of https://api.androidhive.info/contacts/

class AndroidContacts {
 AndroidContact[]  contacts;
}

class AndroidContact {
  String id;
  String name;
  String email;
  String address;
  String gender;
  AndroidPhone phoneDetail;
}

class AndroidPhone {
  String mobile;
  String office;
  String home;
}

AndroidContacts androidContacts;

and then deserialize straight into this structure

androidContacts = (AndroidContacts) JSON.deserialize(response.getBody(),AndroidContacts.class);

Then you can do:

Contact[] contactSObjs = new List<Contact>();
for (AndroidContact c: androidContacts.contacts) {
  contactSObjs.add(new Contact (
    Email = c.email,
    MobilePhone = c.phoneDetail.mobile,
    ...
  ));
}
insert contactSObjs;

I'm leaving out the parsing details for deriving Contact.LastName from the Json name field and breaking up the address field into street/city/state/postalcode

1

You must log in to answer this question.

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