You need to sign in to do that
Don't have an account?
Raghu Sharma 6
process Rest API output
I'm calling external Rest API and recieving the output in the form of JSON. What is the best/easiest way to process this output in Apex?
I see that DOM (Document Object Model) approach only works with XML. Is there any standard function to convert JSON to XML?
Also see that there is a direct way of processing JSON as shown in below URL:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_json_jsonparser.htm
Any sample code to process JSON output will be greatly helpful
I see that DOM (Document Object Model) approach only works with XML. Is there any standard function to convert JSON to XML?
Also see that there is a direct way of processing JSON as shown in below URL:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_json_jsonparser.htm
Any sample code to process JSON output will be greatly helpful
I use the following app to process the JSON resposne for my rest API. Paste the response below and it will generate the class that you can use to deserialze the JSON later. It also creates tests, but i rpefer to write my own. Otherwise the tool is pretty good
https://json2apex.herokuapp.com/
All Answers
I use the following app to process the JSON resposne for my rest API. Paste the response below and it will generate the class that you can use to deserialze the JSON later. It also creates tests, but i rpefer to write my own. Otherwise the tool is pretty good
https://json2apex.herokuapp.com/
public class restResponse
{
public string name;
public List<string> interviews;
public restResponse() { }
}
Type resultType = Type.forName('restResponse');
CustomersResponse deserializeResults = (CustomersResponse)JSON.deserialize(response, resultType);
System.debug('==========> deserialize() results = ' + deserializeResults);
I was able to generate the apex class for the JSON response. I have below questions:
a. Will this class logic work even when there are multiple records returned in JSON. In my case, say 3 contact records are returned in JSON, will this process all of them?
b. How do I leverage this class from other classes? I mean I need to further play around these multiple records in another class and show them in VF page. So, any sample code or idea on leveraging this class will be helpful
Regards
Raghu
a. Yes, you would need to deserialzie it as a list of your wrapper class that was generated.
For example b. Paste it at the end of your controller as an inner class.
String responseStrng = res.getBody();
List<JSON2Apex> n = (List<JSON2Apex>)JSON.deserialize(responseStrng, List<JSON2Apex>.class);
Also, added generated class at the end of controller class as it is.
b. When I tried to save the controller, first I got was below. I overcame by removing static keyword. Not sure, it this impacts the logic
Error: Compile Error: Only top-level class methods can be declared static at line 39 column 28
c. Now, I'm getting below error on compile. Basically this error is thrown on first class inside the generated class 'public class Audit {'
How to overcome this issue?
Error: Compile Error: unexpected token: 'class' at line 54 column 15
Is your inner class located before the last '}'? can you post your code here? the class should like liek following. Try to make it a private class unless you reference it form another class.
I see that based on my JSON, default generated class has more classes inside it which may not be useful. So, I removed them. This fixed the issue.
Now when I try to execute the code, it is throwing the error as below:
System.JSONException: Malformed JSON: Expected '[' at the beginning of List/Set
Did you face similar issues? how to overcome this?
List<JSON2Apex> n = (List<JSON2Apex>)JSON.deserialize(responseStrng, List<JSON2Apex>.class);
Raghu, i guess your wrapper handles the lsit.
Try
JSON2Apex n = (JSON2Apex)JSON.deserialize(responseStrng, JSON2Apex.class);
If it doesnt work can you psot the JSON2Apex class and JSON that you receive?
Also rename your class to something that would make more sense to you.
Glad you found the problem. Please mark this question as resolved then and mark best answer that worked for you.
Thanks,
RZ
did you do the list conversion? Cna you post the wrapper and JSON?
the JSON that you posted for multip[le recordds is not only a lsit of Items records it has other variables such as offset, total, limit, links. I used JSON Parser to generate a new class. Please rename the inner classes adn modify as needed. Instead of deserializing you can jsut call the ItemsWrapper constructor and pass your json in the parameters.
Good luck
It started working after using string functions to extract the data within square brackets for both single and multiple records. Initial testing in progress but hopefully no issues will comeout.