You need to sign in to do that
Don't have an account?
Matty 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?
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?
You can create a wrapper with list of objects like below and deserialize json to the wrapper.
http://json2apex.herokuapp.com/ Here is your JSON 2 Apex Class
Let me know if you need anything else
Thanks,
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?
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.
Thanks,
pRAMODH.
I have the same problem now, Have already solved you your problem?
with your method above, i don't solve my problem, the answer to "system.debug('myClass= ' + myClass)" is:
myClass=null !
JSON2Apex returnSummaryList = JSON2Apex.parse(myJsonString);
use this to loop through your return -> for (JSON2Apex.SummaryList s: JSON2Apex.summaryList) {
//do your logic here
}
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