-1

I have this below JSON - In the below class i want to extract Contact and insert into contact object and accounts in account object.

Can anyone tell how to extract from JSON

{  
   "contact":{  
      "lastname":"Test Wonderland",
      "phone":"1234567890",
      "address":"BTM 2nd Stage",
      "city":"Bangalore",
      "role":"School Admin",
      "state":"Karnataka",
      "zipcode":"560076",
      "email":"test@gmail.com"
   },
   "accounts":[  
      {  
         "account":{  
            "Name":"School Test"
         },
         "quote":[  
            {  
               "gradelevel":"K1",
               "product":"Anmol P1",
               "quantity":"12",
               "price":"33",
               "totalprice":"396"
            }
         ]
      },
      {  
         "account":{  
            "Name":"School Test1"
         },
         "quote":[  
            {  
               "gradelevel":"K2",
               "product":"Anmol P2",
               "quantity":"11",
               "price":"33",
               "totalprice":"363"
            }
         ]
      }
   ]
}

I have created the below class -

@RestResource(urlMapping='/CreateQuote/*')
global with sharing class restcontroller {

   public class Quote {
        public String gradelevel;
        public String product;
        public String quantity;
        public String price;
        public String totalprice;
    }

    public class Account {
        public String Name;
    }

    public class quotewrapper {
        public Contact contact;
        public List<Accounts> accounts;
    }

    public class Accounts {
        public Account account;
        public List<Quote> quote;
    }

    public class Contact {
        public String lastname;
        public String phone;
        public String address;
        public String MailingCity;
        public String role;
        public String state;
        public String zipcode;
        public String email;
    }
     @HttpPost
    global static String createNewQuote() {
    quotewrapper container = (quotewrapper)System.JSON.deserialize(
            RestContext.request.requestBody.tostring(), 
            quotewrapper.class);
    system.debug('container' + container.contact);
    Contact con = container.contact;
    Contact c = new Contact();
    c.lastname = container.contact.lastname;
    insert c;

        return null; 
    }
}
  • 1
    This is a duplicate of the question you asked (and then deleted) yesterday. Like I asked yesterday, what exactly is the issue that you're facing? Your inner classes along with JSON.deserialize() should be doing the job. – Derek F Nov 7 '17 at 14:00
0

you can do it in this way:

public class fromJSON{
public cls_contact contact;
public cls_accounts[] accounts;
class cls_contact {
    public String lastname; //Test Wonderland
    public String phone;    //1234567890
    public String address;  //BTM 2nd Stage
    public String city; //Bangalore
    public String role; //School Admin
    public String state;    //Karnataka
    public String zipcode;  //560076
    public String email;    //test@gmail.com
}
class cls_accounts {
    public cls_account account;
    public cls_quote[] quote;
}
class cls_account {
    public String Name; //School Test
}
class cls_quote {
    public String gradelevel;   //K1
    public String product;  //Anmol P1
    public String quantity; //12
    public String price;    //33
    public String totalprice;   //396
}
public static fromJSON parse(String json){
    return (fromJSON) System.JSON.deserialize(json, fromJSON.class);
}

static testMethod void testParse() {
    String json=        '{  '+
    '   "contact":{  '+
    '      "lastname":"Test Wonderland",'+
    '      "phone":"1234567890",'+
    '      "address":"BTM 2nd Stage",'+
    '      "city":"Bangalore",'+
    '      "role":"School Admin",'+
    '      "state":"Karnataka",'+
    '      "zipcode":"560076",'+
    '      "email":"test@gmail.com"'+
    '   },'+
    '   "accounts":[  '+
    '      {  '+
    '         "account":{  '+
    '            "Name":"School Test"'+
    '         },'+
    '         "quote":[  '+
    '            {  '+
    '               "gradelevel":"K1",'+
    '               "product":"Anmol P1",'+
    '               "quantity":"12",'+
    '               "price":"33",'+
    '               "totalprice":"396"'+
    '            }'+
    '         ]'+
    '      },'+
    '      {  '+
    '         "account":{  '+
    '            "Name":"School Test1"'+
    '         },'+
    '         "quote":[  '+
    '            {  '+
    '               "gradelevel":"K2",'+
    '               "product":"Anmol P2",'+
    '               "quantity":"11",'+
    '               "price":"33",'+
    '               "totalprice":"363"'+
    '            }'+
    '         ]'+
    '      }'+
    '   ]'+
    '}';
    fromJSON obj = parse(json);
    System.assert(obj != null);
   }
  }

now can extract any data from obj.

  • Hi Ram .. can u please let me know how to loop throgh to get contacts each field values.. – user2542953 Nov 7 '17 at 8:44
  • obj.contact.lastname will give you the last name of contact which you can be assigned to your contact object the last name like contact con = new contact(); con.lastName = obj.contact.lastname; – Ram Agarawal Nov 7 '17 at 8:55
  • Hi Ram.. in my above code which now i updated ...when i am trying to insert i am getting error ...DML requires SObject or SObject list type: restcontroller.Contact.. – user2542953 Nov 7 '17 at 9:05
  • your welcome :) – Ram Agarawal Nov 7 '17 at 10:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.