+ Start a Discussion
Matty BMatty B 

how to convert JSON to an Apex Object to render in lightning

Hi. I have the following JSON being returned from a webservice callout and I want to loop over it and put it into a List<Object> via Apex and return that list to my lightning component.

Here is the JSON:

[{"SUMMARY":"testing1","CUSTOMERUSEREMAIL":"xyz@abc.com","CLOSUREDATE":"2011-11-29T11:35:09.000","OWNERNAME":"Jane Doe","CATEGORYNAME":"Finance and Billing","TICKETID":12345,"STATUSDESCRIPTION":"Closed","CREATIONDATE":"2011-01-17T10:16:00.000","CUSTOMERNAME":"CustomerTest123","STATUSNAME":"Closed","SLANAME":"Billing Query","CUSTOMERUSERNAME":"Customer 1","RESOLUTIONDATE":"2011-11-29T11:35:09.000"},
{"SUMMARY":"testing2","CUSTOMERUSEREMAIL":"abc@xyz.com","CLOSUREDATE":"2011-08-03T13:31:48.000","OWNERNAME":"John Smith","CATEGORYNAME":"Operational Request","TICKETID":98765,"STATUSDESCRIPTION":"Closed","CREATIONDATE":"2011-08-02T09:08:00.000","CUSTOMERNAME":"CustomerTest456","STATUSNAME":"Closed","SLANAME":"Terminations","CUSTOMERUSERNAME":"Customer 2","RESOLUTIONDATE":"2011-08-03T09:28:38.000"},
{"SUMMARY":"testing3","CUSTOMERUSEREMAIL":"testemail@test.com","CLOSUREDATE":"2011-09-06T13:19:17.000","OWNERNAME":"Customer 3","CATEGORYNAME":"Operational Request","TICKETID":01287,"STATUSDESCRIPTION":"Closed","CREATIONDATE":"2011-09-06T09:30:00.000","CUSTOMERNAME":"CustomerTest789","STATUSNAME":"Closed","SLANAME":"Activation 1-49","CUSTOMERUSERNAME":"Customer 3","RESOLUTIONDATE":"2011-09-06T13:19:17.000"}]

I have been trying to get this JSON rendered into a list via Apex with which I can display in a lightning component table. Searched all over online and tried various things but all had their own problems. I don't have a real custom SOBJECT for this I just want to use an Apex Object something like List<Object> foo = new List<Object> foo;

I tried things like having their own class... inner class... etc. Then I stumbled upon JSON2Apex (http://json2apex.herokuapp.com/). This generates a nice class and test class. However my problem is I need a list generated and this seems to only create 1 result. Has anyone had success with this? 

I try this where JSON2ApexTickets is my generated JSON2Apex class:

       JSON2ApexTickets myClass = JSON2ApexTickets.parse(res.getBody());

When I dump out the result like this to see the result:
        system.debug('myClass= ' + myClass);

I see the data coming back only for the first record in my JSON list. How do I get it to an object?
SKolakanSKolakan
Matt,
You can create a wrapper with list of objects like below and deserialize json to the wrapper.
 
Public class JSON2ApexTickets
{
Public List<JSON2ApexTicket> tickets {get; set;}
public class JSON2ApexTicket {

	public String SUMMARY;
	public String CUSTOMERUSEREMAIL;
	//other fields

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




 
Matty BMatty B
so i updated my JSON2ApexTickets class with what you have... adding in line 3 and then updating line 13. do I need to change anything on the class that calls this class? I didn't but when this code runs I get the following error: "18:55:10:473 FATAL_ERROR System.JSONException: Malformed JSON: Expected '{' at the beginning of object". My exact JSON is included above in the original post I don't think there is anything wrong with it?
Pramodh KumarPramodh Kumar
You missed "" for the ticket ID. I fixed the your JSON String. copy and paste in the below link

http://json2apex.herokuapp.com/
{
	"summaryList":
	[
	{"SUMMARY":"testing1","CUSTOMERUSEREMAIL":"xyz@abc.com","CLOSUREDATE":"2011-11-29T11:35:09.000","OWNERNAME":"Jane Doe","CATEGORYNAME":"Finance and Billing","TICKETID":"12345","STATUSDESCRIPTION":"Closed","CREATIONDATE":"2011-01-17T10:16:00.000","CUSTOMERNAME":"CustomerTest123","STATUSNAME":"Closed","SLANAME":"Billing Query","CUSTOMERUSERNAME":"Customer 1","RESOLUTIONDATE":"2011-11-29T11:35:09.000"},
	{"SUMMARY":"testing2","CUSTOMERUSEREMAIL":"abc@xyz.com","CLOSUREDATE":"2011-08-03T13:31:48.000","OWNERNAME":"John Smith","CATEGORYNAME":"Operational Request","TICKETID":"98765","STATUSDESCRIPTION":"Closed","CREATIONDATE":"2011-08-02T09:08:00.000","CUSTOMERNAME":"CustomerTest456","STATUSNAME":"Closed","SLANAME":"Terminations","CUSTOMERUSERNAME":"Customer 2","RESOLUTIONDATE":"2011-08-03T09:28:38.000"},
	{"SUMMARY":"testing3","CUSTOMERUSEREMAIL":"testemail@test.com","CLOSUREDATE":"2011-09-06T13:19:17.000","OWNERNAME":"Customer 3","CATEGORYNAME":"Operational Request","TICKETID":"01287","STATUSDESCRIPTION":"Closed","CREATIONDATE":"2011-09-06T09:30:00.000","CUSTOMERNAME":"CustomerTest789","STATUSNAME":"Closed","SLANAME":"Activation 1-49","CUSTOMERUSERNAME":"Customer 3","RESOLUTIONDATE":"2011-09-06T13:19:17.000"}
	]
}
Here is your JSON 2 Apex Class
 
//
// Generated by JSON2Apex http://json2apex.herokuapp.com/
//

public class JSON2Apex {

	public List<SummaryList> summaryList;

	public class SummaryList {
		public String SUMMARY;
		public String CUSTOMERUSEREMAIL;
		public String CLOSUREDATE;
		public String OWNERNAME;
		public String CATEGORYNAME;
		public String TICKETID;
		public String STATUSDESCRIPTION;
		public String CREATIONDATE;
		public String CUSTOMERNAME;
		public String STATUSNAME;
		public String SLANAME;
		public String CUSTOMERUSERNAME;
		public String RESOLUTIONDATE;
	}

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




Let me know if you need anything else


Thanks,
Pramodh.
Matty BMatty B
Hi Pramodh,
So if that JSON is being generated by a web service call is it possible that there is something wrong with the format being returned from that web service? Just fixing the JSON directly won't work I think I need to get to the source of the JSON generation, right?
Pramodh KumarPramodh Kumar
Dont change web service call. If you change any thing that may effect all the other systems which are using same call.


Instead add the string to the JSON, that will rectifies all your problem.

I had same problem before, I just added the string and it worked.


Let me know if you have any other issues.


Thanks,
pRAMODH.
Matty BMatty B
thanks Pramodhd i will look into this and report back. good thing is i actually built the web service call but through a separate integration tool that handles the JSON creation and no other system is using it yet. so i'd like to get to the hear of the problem. i'll talk to that vendor about it first to make sure its generating JSON that is usable by salesforce lists.
Pramodh KumarPramodh Kumar
Please mark as a "Best Answer" if your issue is resolved.


Thanks,
pRAMODH.
tiantian guo 18tiantian guo 18
Hi, Matty B
I have the same problem now, Have already solved you your problem?
tiantian guo 18tiantian guo 18
Hi pRAMODH,
with your method above, i don't solve my problem, the answer to "system.debug('myClass= ' + myClass)" is:
 
myClass=null !
 
Truc NguyenTruc Nguyen
I ran into the same problem, the class returns null. The reason was I didn't have the same name for summaryList in my class and the json string. Make sure they are the same name.


JSON2Apex returnSummaryList = JSON2Apex.parse(myJsonString);

use this to loop through your return -> for (JSON2Apex.SummaryList s: JSON2Apex.summaryList) {
   //do your logic here
}
Suraj Tripathi 47Suraj Tripathi 47
Hi Matty B,

Greetings!

To convert your JSON into apex object, First create a Wrapper class, and create instance variable in wrapper class which we want to convert.
Now, Create an object of your Wrapper class and use JSON.deserialize method to convert your data into an object.
for example-
MyWrapper wrapperObj = (MyWrapper) JSON.deserialize(jsonBody, MyWrapper.class);

If you find your Solution then mark this as the best answer. 

Thank you!

Regards,
Suraj Tripathi