+ Start a Discussion
Shiv786Shiv786 

Read JSON from online API

Hi All

 

Following is an open link that can be used to get the Capital Markets data from the web.

 

Say for APPLE SOFTWARES:

 

http://dev.markitondemand.com/Api/Quote/json?symbol=AAPL

 

(where AAPL in the URL is the symbol for APPLE)

 

I wish to directly hit this link, that gives me response in JSON format and then convert it into my apex class object fields.

 

Is it possible???

 

Can anybody help???

 

Thanks in Advance!!!

 

Shiv

vbsvbs

Shiv - You can use native SF HTTP classes and methods to implement this. A simple flow for this would be:
1. Set up the HTTPRequest object with the correct endpoint. In this case - http://dev.markitondemand.com/Api/Quote/json
2. Set the method as GET. Append the query parameter symbol dynamically to this request.
3. Set up the HTTP object and pass the request as parameter and call the send method.
4. The send method returns an HTTPReponse object. Use the getBody from the reponse which should be a simple JSON String.

5. To deserialize it into a SFDC class from the JSON string use the JSON class methods.

 

Note: Do not forget to add the external URL to Remote Access to allow outbound calls from SFDC.

 

This is a rough template on accessing REST resources from SFDC. In case the external app starts using an API key, you can copy this into a custom setting and then add this to the HTTPRequest Authorization header, in your code, in the future. But as I see it the resource is currently openly available from access from a browser.

georggeorg

For parsing response JSON object into APEX class plese visit the below blog

 

http://learnsfdc.blogspot.com/2013/07/how-to-utilize-deserialize-method-to.html

 

Thanks