Activity Forums Salesforce® Discussions Conversion of JSON Format attributes to Salesforce Sobjects Reply To: Conversion of JSON Format attributes to Salesforce Sobjects

  • sushant

    Member
    January 16, 2017 at 2:24 pm

    Hi Vikas,

    The format for conversion is as follows:

    public class JSON2Apex {

    public class Attributes {
    public String type;
    public String url;
    }

    public class Books {
    public Attributes attributes;
    public String Name;
    public String Id;
    public String Author__c;
    }

    public List<Books> Books;
    public static JSON2Apex parse(String json) {
    return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
    }

    static testMethod void testParse() {
    String json = '{ \"Books\" : [ { \"attributes\" : { \"type\" : \"Book__c\", \"url\" : \"/services/data/v30.0/sobjects/Book__c/a019000000CFwyNAAT\" }, \"Name\" : \"C fundamental\", \"Id\" : \"a019000000CFwyNAAT\", \"Author__c\" : \"Bharat\" },'+
    '{ \"attributes\" : { \"type\" : \"Book__c\", \"url\" : \"/services/data/v30.0/sobjects/Book__c/a019000000CFwycAAD\" }, \"Name\" : \"Network Security\", \"Id\" : \"a019000000CFwycAAD\", \"Author__c\" : \"Vikram\" },'+
    '{ \"attributes\" : { \"type\" : \"Book__c\", \"url\" : \"/services/data/v30.0/sobjects/Book__c/a019000000CFwyIAAT\" }, \"Name\" : \"Core Java\", \"Id\" : \"a019000000CFwyIAAT\", \"Author__c\" : \"Rohit\" },'+
    '{ \"attributes\" : { \"type\" : \"Book__c\", \"url\" : \"/services/data/v30.0/sobjects/Book__c/a019000000CFwy3AAD\" }, \"Name\" : \"salesforce\", \"Id\" : \"a019000000CFwy3AAD\", \"Author__c\" : \"Amit\" },'+
    '{ \"attributes\" : { \"type\" : \"Book__c\", \"url\" : \"/services/data/v30.0/sobjects/Book__c/a019000000CG020AAD\" }, \"Name\" : \"SFDC admin\", \"Id\" : \"a019000000CG020AAD\", \"Author__c\" : \"Om\" } ] }';
    JSON2Apex obj = parse(json);
    System.assert(obj != null);
    }
    }

    Hope this helps you