+ Start a Discussion
Merry SMerry S 

System.JSONException: Malformed JSON: Expected '{' at the beginning of object

I've created a class to make a callout, the JSON seems to be formatted right, but when trying to deserialize it I get the above error. What am I doing wrong?

Here is the debug log showing how the request is fomatted:
17:54:08.083 (83420512)|USER_DEBUG|[67]|DEBUG|jsonOrders: {
  "entities" : [ {
    "attributes" : {
      "type" : "DatacloudCompany"
    "Name" : "GE Healthcare Institute",
    "Country" : "United States",
    "Phone" : "(262) 951-2047"
  } ],
  "matchoptions" : {
    "fields" : "City, CompanyId, Country, Description, DunsNumber, Name, Website, YearStarted, Zip",
    "matchEngine" : "DunsRightMatchEngine",
    "maxMatchResults" : "6",
    "minMatchConfidence" : "5",
    "rule" : "DunsRightMatchRule",
    "sObjectType" : "DatacloudCompany"
This seems to be right based on the dev guide https://developer.salesforce.com/docs/atlas.en-us.datadotcom_api_dev_guide.meta/datadotcom_api_dev_guide/request_understanding_a_request.htm#request_understanding_a_request

However when it gets to this line in my code I get the error:
Entities ddcdata =
                    System.debug('ddcdata: ' + ddcdata);

Here is the full class:
public class Lead_fromJSON{

	public class Attributes {
		public String Type {get;set;}

	public class Entities {
		public Attributes Attributes;
		public String Name {get;set;}
		public String Country {get;set;}
		public String Phone {get;set;}

	public class Matchoptions {
		public String fields;
		public String matchEngine {get;set;}
		public String maxMatchResults {get;set;}
		public String minMatchConfidence {get;set;}
		public String rule {get;set;}
		public String sObjectType {get;set;}

	public List<Entities> Entities;
	public Matchoptions matchoptions;

     @future (callout=true) // indicates that this is an asynchronous call
    public static void postOrder(List<String> leadIds) {

     List<Lead> ld = [SELECT Id, Company, Country, phone FROM Lead where Company IN :leadIds];
        System.debug('leadinfo: ' + ld);
         for (Lead l : ld)    {
    	        // Create a JSON generator object
        JSONGenerator gen = JSON.createGenerator(true);
        // open the JSON generator

            gen.writeStringField('type', 'DatacloudCompany');
            gen.writeStringField('Name', l.Company);
            gen.writeStringField('Country', l.Country);
            gen.writeStringField('Phone', l.phone);
            gen.writeStringField('fields', 'City, CompanyId, Country, Description, DunsNumber, Name, Website, YearStarted, Zip');
            gen.writeStringField('matchEngine', 'DunsRightMatchEngine');
            gen.writeStringField('maxMatchResults', '6');
            gen.writeStringField('minMatchConfidence', '5');
            gen.writeStringField('rule', 'DunsRightMatchRule'); 
            gen.writeStringField('sObjectType', 'DatacloudCompany');           

        // close the JSON generator

        // create a string from the JSON generator
        String jsonOrders = gen.getAsString();
        // debugging call, which you can check in debug logs
        System.debug('jsonOrders: ' + jsonOrders);

        // create an HTTPrequest object    
        HttpRequest req = new HttpRequest();
        // set up the HTTP request with a method, endpoint, header, and body
        req.setHeader('Content-Type', 'application/json');
        // create a new HTTP object
        Http http = new Http();
        // create a new HTTP response for receiving the remote response
        // then use it to send the configured HTTPrequest
        HTTPResponse res = http.send(req);

            // Retrieve all of the Lead records
            // originally passed into the method call to prep for update.           
            List<Lead> leads =
              [SELECT Id FROM Lead WHERE Id IN :ld];
            // Create a list of entities by deserializing the
            // JSON data returned by the fulfillment service.
            Entities ddcdata =
                    System.debug('ddcdata: ' + ddcdata);
            // Create a map of Lead IDs from the retrieved
            // invoices list.
            Map<String, Lead> leadMap =
              new Map<String, Lead>(leads);

              Lead lead = leadMap.get(ddcdata.Name);
              if (ddcdata.Name != null)
              lead.Company = String.valueOf(ddcdata.Name);
              lead.phone = String.valueOf(ddcdata.phone);
            // Update all leads in the database with a bulk update
            update leads;

From Debug log, the respose is not starting with {
So it is not valid Json.
1) Try to append { to Response body at start as well as at the end as needed. or
removed the string jsonOrders: from the response body

This type of error is because of "/" in JSON string, sometime "/" is appending with your JSON string automatically like below.

Original JSON: [{"Attribute1":"Value1","Attribute2":"Value2","Attribute3":"Value3"}]
Invalid JSON:   [{/"Attribute1/":/"Value1/",/"Attribute2/":/"Value2/",/"Attribute3/":/"Value3/"}]

Invalid JSON have "/" before all double quotes ("), if you try to read you JSON in Javascript alert, then you'll find these type of invalid JSON.

$(document).ready(function() {
	// Resolve issue of json string apped backslash before double quote automatically
	if(window.Prototype) {
		delete Object.prototype.toJSON;
		delete Array.prototype.toJSON;
		delete Hash.prototype.toJSON;
		delete String.prototype.toJSON;


Thanks and Cheers,
prithvi kumarprithvi kumar
It expects { at the begining means ,you're not using a list ,hence [ is not necessary.

"entities" : {
   "attributes" : {
      "type" : "DatacloudCompany"
    "Name" : "GE Healthcare Institute",
    "Country" : "United States",
 "Phone" : "(262) 951-2047"
 } ,
"matchoptions" : {
   "fields" : "City, CompanyId, Country, Description, DunsNumber, Name, Website, YearStarted, Zip",
    "matchEngine" : "DunsRightMatchEngine",
   "maxMatchResults" : "6",
  "minMatchConfidence" : "5",
  "rule" : "DunsRightMatchRule",
    "sObjectType" : "DatacloudCompany"

Hope this shoyuld resolve this error.