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
Kishore B T 21Kishore B T 21 

Cannot Deserialize a Json String?

Please guide me with Json As i am new to SF -> Json 
I am executing this in Developer Console. 

string leads ='[{"FirstName":"Kishore","LastName":"B T","Company":"IPL","City":"Bangalore",'+
 '   "State":"KA","PostalCode":560068,"Phone":8105690988,"Status":"Open - Not Contacted",'+
     '   "Tags":[]},{"FirstName":"deeps","LastName":"O M","Company":"vv",  '+
        '    "City":"Bangalore","State":"KA","PostalCode":560078,"Phone":95055556343,'+
            '    "Status":"Open - Not Contacted","Tags":[]} ]'
;

String leadsJSON = JSON.serializePretty(leads);
system.debug('accountsDeserialized ++ '+leadsJSON);

List<lead> accountsDeserialized = (List<lead> ) JSON.deserialize(leadsJSON, List<lead>.class);
system.debug('accountsDeserialized ++ '+accountsDeserialized);

I have validated this json in http://json2apex.herokuapp.com/but i get this error

**System.JSONException: Malformed JSON: Expected '[' at the beginning of List/Set**

Let me know Where I am going wrong.
SRKSRK
U can 1st try this

 list<lead> updateLeadList = new list<lead>();
updateLeadList = [select id,name,FirstName,LastName limit 5];

system.debug('$%^$%^$%^$'+JSON.serialize(updateLeadList));

so you can see how exactly JSON support by salesforce mean while i am look into it for more :)
Amit Chaudhary 8Amit Chaudhary 8
I hope you are usinge below Apex class as wrapper
 
//
// Generated by JSON2Apex http://json2apex.herokuapp.com/
//

public class JSON2Apex {

	public String FirstName;
	public String LastName;
	public String Company;
	public String City;
	public String State;
	public Integer PostalCode;
	public Long Phone;
	public String Status;
	public List<Tags> Tags;

	public class Tags {
	}

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

 
SRKSRK

wrapper class is required when you have custom structure of you only using lead,it do not need to use 
wrapper class

i belvie it is the error just because of JSOn formate accept by salesforce that we can confirm by running 

list<lead> updateLeadList = new list<lead>();
updateLeadList = [select id,name,FirstName,LastName limit 5];

system.debug('$%^$%^$%^$'+JSON.serialize(updateLeadList));

Kishore B T 21 please try that and let me know
Kishore B T 21Kishore B T 21
Hi Amit, 

Even i have done the same but its working for 1 Json record entries but how do we do the same for multiple JSon records,

Lets take a Example only for Firstname ,Lastname and company,

[
    {
        "FirstName": "aaa",
        "LastName": "aaa",
        "company": "aaa"
    },
    {
        "FirstName": "bbb",
        "LastName": "bbb",
        "company": "bbb"
    }
]

How to send this as a string? I get the Malformed error when I use Square brackets at top and bottom?
Kishore B T 21Kishore B T 21
Hi SRK,

My question is how deserialize the string if there are multiple records,
I can serialize the records by querying as you have suggested but how to deserialize the same?