You need to sign in to do that
Don't have an account?
Kishore 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.
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.
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 :)
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
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?
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?