function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
divya manohardivya manohar 

json error

{ "rb": { "a": {
			"Name": "LG1",
			"SLAExpirationDate__c": "2016-4-21",
			"Active__c": "Yes",
			"SLA__c": "Platinum",
			"SLASerialNumber__c": "200"
		},
		"cList": [{
			"FirstName": "K1",
			"LastName": "K2"
		}, {
			"FirstName": "K3",
			"LastName": "K4"
		}]
	},
	{
			"Name": "LG2",
			"SLAExpirationDate__c": "2016-4-21",
			"Active__c": "Yes",
			"SLA__c": "Platinum",
			"SLASerialNumber__c": "200"
		},
		"cList": [{
			"FirstName": "K5",
			"LastName": "K6"
		},
                {
			"FirstName": "K7",
			"LastName": "K8"
		}]
	
}
Hello 
I am getting an error in above json file.
for multiple accounts I have multiple contacts.

divya
 
Daniel BallingerDaniel Ballinger
What API call are you making with this? I.e. which REST API resource are you sending it to.

Also, what is the error you are getting?
divya manohardivya manohar
Hi daniel
I am not making any api call.
I am testing the rest resource in workbench.
I am creating multiple accounts and for each account I have multiple contacts.
@RestResource(urlMapping='/BulkInsertaccountcontact/*')
global class Bulkinsert_accountcontact
{
   global class RequestBody 
   {
       Account a; //create single account
       List<Contact> cList;
   }
   
   global class myWrapper
   {
      List<Account> a;
      List<Contact> cList;
      public string status;
      public string message;
   }
   
   @HttpPost
   global static myWrapper doPost(RequestBody rb)
   {
      RestRequest req=RestContext.request;
      RestResponse res=RestContext.response;
   
      myWrapper response=new myWrapper(); //this is object of wrapper class
      
      List<Account> myList=new List<Account>();
      
      try
      {
        myList.add(rb.a);
        
        response.a=myList;
        
        for(Contact con:rb.cList)
        {
           con.AccountID=rb.a.ID;
        }
        
        //we need to insert contact List also
        Insert rb.cList;
        response.cList=rb.cList;
        response.status='Success';
        response.message='Created Successfully';
        
      }
      catch(exception ex)
      {
         response.a=null;
         response.cList=null;
         response.status='ERROR';
         response.Message='could not create record'+ ex.getMessage();
      }
      
      return response;
   }
}



{ "rb": { "a": {
			"Name": "LG1",
			"SLAExpirationDate__c": "2016-4-21",
			"Active__c": "Yes",
			"SLA__c": "Platinum",
			"SLASerialNumber__c": "200"
		},
		"cList": [{
			"FirstName": "K1",
			"LastName": "K2"
		}, {
			"FirstName": "K3",
			"LastName": "K4"
		}]
	},
	{
			"Name": "LG2",
			"SLAExpirationDate__c": "2016-4-21",
			"Active__c": "Yes",
			"SLA__c": "Platinum",
			"SLASerialNumber__c": "200"
		},
		"cList": [{
			"FirstName": "K5",
			"LastName": "K6"
		},
                {
			"FirstName": "K7",
			"LastName": "K8"
		}]
	
}

Error:
message: Unexpected character ('{' (code 123)): was expecting double-quote to start field name at [line:16, column:3]
errorCode: JSON_PARSER_ERROR

divya
 
Daniel BallingerDaniel Ballinger
I ran your sample JSON through an online JSON Formatter and Validator (https://jsonformatter.curiousconcept.com/)

It came back with the error: 
INVALID JSON (RFC 4627)
Validator Output
Error:Expecting string, not {.[Code 8, Structure 54]

This corresponds to just before the:
 
        { 
            "Name": "LG2",
            "SLAExpirationDate__c": "2016-4-21",
            "Active__c": "Yes",
            "SLA__c": "Platinum",
            "SLASerialNumber__c": "200"
        }

I suspect you need a string to preceed this. Something like:
 
"foobar" : { 
            "Name": "LG2",
            "SLAExpirationDate__c": "2016-4-21",
            "Active__c": "Yes",
            "SLA__c": "Platinum",
            "SLASerialNumber__c": "200"
        }

 
divya manohardivya manohar
Hi daniel
I ran ur suggestion in workbench but its not working.
error:
message: Unexpected parameter encountered during deserialization: foobar at [line:15, column:14]
errorCode: JSON_PARSER_ERROR
Daniel BallingerDaniel Ballinger
Ah, I see now. The JSON that you are passing in needs to match you RequestBody class. I should have fields for a single Account "a" and "cList" as a collection of Contacts. Try modifying your JSON payload to something like:
 
{
   "rb":{
      "a":{
         "Name":"LG1",
         "SLAExpirationDate__c":"2016-4-21",
         "Active__c":"Yes",
         "SLA__c":"Platinum",
         "SLASerialNumber__c":"200"
      },
      "cList":[
         {
            "FirstName":"K1",
            "LastName":"K2"
         },
         {
            "FirstName":"K3",
            "LastName":"K4"
         }
      ]
   }
}
divya manohardivya manohar
Hi 
I have the done the above previously and its working.
But what I require is multiple account and contact, so I need to have another  "a" folowed by "cList".
Daniel BallingerDaniel Ballinger
Your doPost method is only taking a single RequestBody. You would need to modify it to take a List of RequestBody records. Then update your JSON accordingly. 
Abhishek Ranjan 10Abhishek Ranjan 10

The json format you are using is not correct. This one is correct json. Please check the BOLD one.

{
    "rb": {
        "a": {
            "Name": "LG1",
            "SLAExpirationDate__c": "2016-4-21",
            "Active__c": "Yes",
            "SLA__c": "Platinum",
            "SLASerialNumber__c": "200"
        },
        "cList": [{
            "FirstName": "K1",
            "LastName": "K2"
        }, {
            "FirstName": "K3",
            "LastName": "K4"
        }]
    },
    "b": {
        "Name": "LG2",
        "SLAExpirationDate__c": "2016-4-21",
        "Active__c": "Yes",
        "SLA__c": "Platinum",
        "SLASerialNumber__c": "200"
    },
    "cList": [{
        "FirstName": "K5",
        "LastName": "K6"
    }, {
        "FirstName": "K7",
        "LastName": "K8"
    }]

}