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
SFDC pvSFDC pv 

How to convert JSON to apex format. I'm getting internal server error when i tried to deserialize. Please help

@AuraEnabled
    public static String insertinfo(String caseId, String accountId, List<String> results)
    {
        system.debug('ffmap'+results);    
        list<objectWrap> myWrap = (list<objectWrap>)results;    //Getting error here . Also ffmap is not coming in the valid format 
 }
ffmap log :- ({key=123, value={key1=KEITH W PETERSON, key2=123, key3=null, key4=Bronze, key5=31-Jan-2019}}, {key=456, value={key1=JOYCE P WEST, key2=456, key3=xdkzwhzorm@uaowz.qca, key4=Bronze, key5=31-Jul-2018}})

From helper i am getting proper json format whereas in the apex ctrlr am getting the result in valid format
My helper class :- 

CreateHelper: function(component, event) {
        //call apex class method
        var action = component.getinsertinfo
            var myMap = component.get("v.selectednos");
            console.log('Mymapvalue' + myMap);
            action.setParams({
                "caseId": component.get("v.CaseId"),
                "accountId" : component.get("v.AccountId"),
                "results" : myMap
            });

log:-
Mymapvalue'  :-   [{"key":"123","value":{"key1":"KEITH W PETERSON","key2":"123","key3":null,"key4":"Bronze","key5":"31-Jan-2019"}},{"key":"456","value":{"key1":"JOYCE P WEST","key2":"456","key3":"xdkzwhzorm@uaowz.qca","key4":"Bronze","key5":"31-Jul-2018"}}]
Mario PariseMario Parise
1) Change your apex class method definition from:
public static String insertinfo(String caseId, String accountId, List<String> results)
to:
public static String insertinfo(String caseId, String accountId, String results)
...because JSON is sent to your APEX as a string, not as a list.

2) What type of data does your JSON hold? It looks like a custom object of the following format:
Map<String, Map<String, String>>
Is that correct? It's a map of maps of strings? If so, you would process your JSON as such:
Map<String, Map<String, String>> myResults = (Map<String, Map<String, String>>)JSON.deserialize( results, Map<String, Map<String, String>>.class );
What this is doing is:

1) Declaring that myResults is an object that will contain a map, and that map will contain maps, and those maps will be of strings. You shouldn't need a wrapper class for this purpose.
2) It deserializes your string into that format.

I believe that should do it.
SFDC pvSFDC pv
Hi Mario,

Would it be possible to directly iterate the results like below if i get valid json format from ightning java script  ctrlr

public static String addParentCase(String caseId, String accountId, string results)
 for(String myff : results) {
}
Mario PariseMario Parise
Not quite. In order to iterate, you need a list.

Your data arrives as a String, which needs to be processed as JSON and converted into an appropriate object. Since your data is a Map of Maps, it's not a list either.

What you *could* do is after you've converted your string into a Map of Maps:
for ( String key : myResults.keySet() ) {
    Map<String, String> myMap = myResults.get(key);
}
This will loop through every object within myResults and make it available to you as myMap.

Based on your sample data, it would look like this:
String name = myMap.get('key1');
String pin = myMap.get('key2');
String email = myMap.get('key3');
String color = myMap.get('key4');
String myDate = myMap.get('key5');
Putting it all together:
@AuraEnabled
public static String insertinfo(String caseId, String accountId, String results) {
    Map<String, Map<String, String>> myResults = (Map<String, Map<String, String>>)JSON.deserialize( results, Map<String, Map<String, String>>.class );
    for ( String key : myResults.keySet() ) {
        Map<String, String> myMap = myResults.get(key);
        String name = myMap.get('key1'); 
        String pin = myMap.get('key2'); 
        String email = myMap.get('key3');
        String color = myMap.get('key4'); 
        String myDate = myMap.get('key5');
        // Now do whatever you like with name, pin, email, color, and myDate.
    }
}
Mario PariseMario Parise
...don't forget to return a String at the end. If you wanted to return your map, for example, you would add the following at the end of your method:
return JSON.serialize(myResults);
Whatever you pass into JSON.serialize() will be a string.
Mario PariseMario Parise
The problem is that it's failing to parse the JSON. Once it'd parsed, I believe the iteration should be okay.

I believe it's struggling with the opening '[' in your json string. Could you try remove the brackets from the beginning and end of your json string before sending it to the apex? They can also be stripped within the apex, but you might as well give it clean data right up front.
Mario PariseMario Parise
You're converting your JSON into a list of objects. Then you're looping through that list, so "fld" is an Object. Then you're trying to say "fld is actually a Map<String, Object>".

So your Apex error is telling you "You already told me this is an object. You can't change your mind and tell me it's a map of strings and objects."