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
bharath kumar 52bharath kumar 52 

How to parse list <object> sent from lightning component?

Hi All,

I am working on building a search component with filters which are passed by end users. Although i am able to capture the filters and send them to my apex class, I am not able to convert the list<object> to json or a wrapper in order to build my query.I have also given the input data formats that i receive in the code below. Can some one let me know as to how i can convert the list<object> to list<string> or list<wrapper> ?
 

// json sent by lightning component
[{"fieldNames":"test","criteriaType":"91102444","searchStr":"123123"},{"fieldNames":"Pasqua","criteriaType":"911032323","searchStr":"123"}]


//How the json gets converted and looks in debug logs currently
20:50:23:004 USER_DEBUG [11]|DEBUG|prodWrapList >>>> ({criteriaType=91102444, fieldNames=test, searchStr=123123}, {criteriaType=911032323, fieldNames=Pasqua, searchStr=123})

public class SearchFilterController {
	@AuraEnabled
    public static void saveAccountList(List<Account> accList) 
	{        
        Insert accList;
    }
    
    @AuraEnabled
    public static void generateQuery(List<object> prodWrapList) 
	{        
		system.debug('prodWrapList >>>> '+prodWrapList.tostring());
        system.debug('prodWrapList size >>>> '+prodWrapList.size());
        list<String> queryParams=(list<String>)prodWrapList;
        List<productWrapper> lstWrapper = new List<productWrapper>();
        
        //lstWrapper.addAll((List<productWrapper>)prodWrapList);
        //JSON.deserializeUntyped((list<string>)prodWrapList); ---->FATAL_ERROR System.TypeException: Invalid conversion from runtime type List<ANY> to List<String>
        //JSON.deserialize(prodWrapList, lstWrapper); ----> doesn't work
        system.debug('deserialized lst >>>> '+lstWrapper);
    }
    
 
    
    
        public class productWrapper{
            @AuraEnabled
            public String searchStr{get; set;}
            @AuraEnabled
            public Map<String,String> fieldNames{get; set;}
            @AuraEnabled
            public list<String> criteriaType{get; set;}
            
            public productWrapper(){
                criteriaType= new list<String>();
                criteriaType.add('equal to');
                criteriaType.add('greater than or equal to');
                criteriaType.add('lesser than or equal to');
                criteriaType.add('lesser than');
                criteriaType.add('greater than');
                criteriaType.add('contains');
                
                Map<String, Schema.SObjectField> schemaFieldMap = Schema.SObjectType.Product2.fields.getMap();
                //Map<String, Object> queriedFieldValues = new Map<String, Object>();
                for (String fieldName: schemaFieldMap.keySet()) {
                    try { 
                        fieldNames.put(fieldName, fieldName); 
                    } 
                    catch(Exception e){
                        
                    }
            }
        }
    }
}
AbhinavAbhinav (Salesforce Developers) 
Hi Bharat,

Have you checked suggestion on this discussion?

https://salesforce.stackexchange.com/questions/126212/how-to-convert-list-data-into-json-in-apex

JSON Class
Contains methods for serializing Apex objects into JSON format and deserializing JSON content that was serialized using the serialize method in this class.

https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_System_Json.htm

Thanks!