function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Siddhartha Tomar 12Siddhartha Tomar 12 

how to add response in lightning component

I have one requirement where i am getting the below response from third party and now i wanted to show the response field on lightning component . Can any one help me how to desrialized it into list so that i can add it on lightning component
{
    "rates": [
        {
            "advertised": 0.0299,
            "combined": 0,
            "from_amt": 0,
            "to_amt": 0.1599,
            "code": "ABC1899",
            
        },
        {
            "advertised": 0.0299,
            "combined": 0,
            "from_amt": 0,
            "to_amt": 0.1599,
            "code": "ABC1899",
            
        },
        {
            "advertised": 0.0299,
            "combined": 0,
            "from_amt": 0,
            "to_amt": 0.1599,
            "code": "ABC1899",
           
        },
        {
            "advertised": 0.0299,
            "combined": 0,
            "from_amt": 0,
            "to_amt": 0.1599,
            "code": "ABC1899",
           
        }
    ],
  
}
Khan AnasKhan Anas (Salesforce Developers) 
Hi Siddhartha,

Greetings to you!

You can use JSON.parse in the client-side controller or you can use JSON.deserialize in server-side controller. Please refer to the below links which might help you further with the above requirement.

https://salesforce.stackexchange.com/questions/169507/getting-the-json-values-in-a-component

https://salesforce.stackexchange.com/questions/175170/parse-a-json-and-use-it-in-lightning

https://salesforce.stackexchange.com/questions/160846/is-there-any-function-to-deserialize-an-json-to-an-array-in-lightning-component

https://salesforce.stackexchange.com/questions/214224/how-can-i-parse-my-json-blob-in-my-lightning-component

https://developer.salesforce.com/forums/?id=9060G0000005XPPQA2

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Raj VakatiRaj Vakati
There are three different ways .. 

1 . You can parse it in apex class using json parsing and return the wrapper class to ui and its easy to do it 

2 . Parsing by using the Javascript Parsing .. i will not recommend this because you need to do a lot of loops ( using ES6 maps ,forEach ect )

3 . Set the json object in attribute and using the  aura:iteration to loop though the values 


https://salesforce.stackexchange.com/questions/202701/json-parsing-and-aura-iteration-in-salesforce-lightning/202704

https://developer.salesforce.com/index.php?title=Getting_Started_with_Apex_JSON
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_json_jsonparser.htm
http://www.salesforcenextgen.com/how-to-do-json-parsing-of-a-wrapper-class-in-salesforce/
http://bobbuzzard.blogspot.com/2015/07/lightning-components-and-custom-apex.html
http://www.infallibletechie.com/2017/07/how-to-display-wrapper-class-list-in.html

 
public class WrapperClass {   
    
    public class VehicleWrapper {
        @AuraEnabled
        public String MakeId {get;set;}
        @AuraEnabled
        public String MakeName {get;set;}
        @AuraEnabled
        public String VehicleTypeId {get;set;}
        @AuraEnabled
        public String VehicleTypeName {get;set;}
    }
 
public with sharing class DPLightningHandler {

    @AuraEnabled
    public static List < WrapperClass.VehicleWrapper > fetchInventory(String vehicleMake) {
        List < WrapperClass.VehicleWrapper > listVehicles = new List < WrapperClass.VehicleWrapper >();
        HTTPRequest req = new HTTPRequest();
        req.setEndPoint('https://vpic.nhtsa.dot.gov/api/vehicles/GetVehicleTypesForMake/' + vehicleMake + '?format=json');
        req.setMethod('GET');
        HTTP objHTTP = new HTTP();
        HTTPResponse res = objHTTP.send(req);
        system.debug('Response is ' + res.getBody());
        JSONParser parser = JSON.createParser(res.getBody());
        System.JSONToken token;
        String text;
        parser.nextToken(); // Eat first START_OBJECT {
        parser.nextToken(); // Eat token = FIELD_NAME; text = postalcodes
        parser.nextToken(); // Eat first START_ARRAY [
        parser.nextToken(); // Eat the first object's START_OBJECT {
        WrapperClass.VehicleWrapper obj;
        while ( ( token = parser.nextToken()) != null ) {
            if ( ( token = parser.getCurrentToken() ) != JSONToken.END_OBJECT ) {
                text = parser.getText();
                if ( token == JSONToken.FIELD_Name && text == 'MakeId' ) {
                    token = parser.nextToken();
                    obj = new WrapperClass.VehicleWrapper();
                    obj.MakeId = parser.getText();
                } else if (token == JSONToken.FIELD_Name && text == 'MakeName' ) {
                    token = parser.nextToken();
                    obj.MakeName = parser.getText();
                } else if ( token == JSONToken.FIELD_Name && text == 'VehicleTypeId' ) { 
                    token = parser.nextToken();
                    obj.VehicleTypeId = parser.getText();
                } else if ( token == JSONToken.FIELD_Name && text == 'VehicleTypeName' ) {
                    token = parser.nextToken();
                    obj.VehicleTypeName = parser.getText();
                    listVehicles.add(obj);
                } 
            } 
        }
        system.debug('listVehicles are ' + listVehicles);
        return listVehicles;
    }
    
}