+ Start a Discussion
Diwakar G 7Diwakar G 7 

Reading values of JSON in Apex

Hi,

How to read the values of below json using List and Maps in Apex. 
String recordstring = '[[{"File__c":"classes/VerifyDate.cls"},{"Rule__c":"MethodNamingConventions"},{"Message__c":"Method names should not start with capital letters"}],[{"File__c":"classes/VerifyDate.cls"},{"Rule__c":"MethodNamingConventions"},{"Message__c":"Method names should not start with capital letters"}],[{"File__c":"classes/VerifyDate.cls"},{"Rule__c":"MethodNamingConventions"},{"Message__c":"Method names should not start with capital letters"}]]'
Please help me.

Thanks and Regards,
Diwakar G
 
Best Answer chosen by Diwakar G 7
Maharajan CMaharajan C
Hi Diwakar,

Try the below updated one:

String recordstring = '[[[{"usname":"US-002"},{"to":"Diw"}]],[[{"File__c":"classes/VerifyDate.cls"},{"Rule__c":"MethodNamingConventions"},{"Message__c":"Method names should not start with capital letters"}],[{"File__c":"classes/VerifyDate.cls"},{"Rule__c":"MethodNamingConventions"},{"Message__c":"Method names should not start with capital letters"}],[{"File__c":"classes/VerifyDate.cls"},{"Rule__c":"MethodNamingConventions"},{"Message__c":"Method names should not start with capital letters"}]]]';

List<Object> results = (List<Object>)JSON.deserializeUntyped(recordstring);
Map<String,Object> keymap = new Map<String,Object>();
List<Object> objList2 = new List<Object>();
for(Object obj:results)
{
    List<Object> objList = (List<Object>)obj;
    for(Object obj1 : objList)
    {
        objList2.add(obj1);
    }
}

for(Object obj2:objList2 )
{
    List<Object> objList1 = (List<Object>)obj2;
    
    for(Object obj3:objList1)
    {
        Map<String,Object> keyStr = (Map<String,Object>)obj3;
        if(keyStr.containsKey('usname'))
            system.debug('@@@ usname  ==> ' + keyStr.get('usname')); 
        else if(keyStr.containsKey('to'))
            system.debug('@@@ to  ==> ' + keyStr.get('to'));
        else if(keyStr.containsKey('File__c'))
            system.debug('@@@ File__c  ==> ' + keyStr.get('File__c'));
        else if(keyStr.containsKey('Rule__c'))
            system.debug('@@@ Rule__c  ==> ' + keyStr.get('Rule__c'));
        else if(keyStr.containsKey('Message__c'))
            system.debug('@@@ Message__c  ==> ' + keyStr.get('Message__c'));    
    }
    
}

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Maharajan.C

All Answers

Abdul KhatriAbdul Khatri
The way I can think of is like this. You can run in the Anonymous Windows.
 
Public class SampleClass {
	public string File;
    public string Rule;
    public string Message;
}

List<SampleClass> scList = new List<SampleClass>();
SampleClass sc;

String recordstring = '[[{"File__c":"classes/VerifyDate.cls"},{"Rule__c":"MethodNamingConventions"},{"Message__c":"Method names should not start with capital letters"}],[{"File__c":"classes/VerifyDate.cls"},{"Rule__c":"MethodNamingConventions"},{"Message__c":"Method names should not start with capital letters"}],[{"File__c":"classes/VerifyDate.cls"},{"Rule__c":"MethodNamingConventions"},{"Message__c":"Method names should not start with capital letters"}]]';

System.JSONParser parser = JSON.createParser(recordstring);
while (parser.nextToken() != null) {
    if(parser.getCurrentToken() != JSONToken.FIELD_NAME) continue;
    
    if(parser.getText() == 'File__c')
    {
        parser.nextToken();
        sc = new SampleClass();
        sc.File = parser.getText();         
    }

    if(parser.getText() == 'Rule__c')
    {
        parser.nextToken();
        sc.Rule = parser.getText();         
    }    

    if(parser.getText() == 'Message__c')
    {
        parser.nextToken();
        sc.Message = parser.getText();  
        scList.add(sc);
    }        
}
system.debug(scList);
Please mark it a best if able to resolve your issue.

Thanks
 
Maharajan CMaharajan C
Hi Diwakar,

If you want to use the List and Map to Deserialize then refer the below code :

String recordstring = '[[{"File__c":"classes/VerifyDate.cls"},{"Rule__c":"MethodNamingConventions"},{"Message__c":"Method names should not start with capital letters"}],[{"File__c":"classes/VerifyDate.cls"},{"Rule__c":"MethodNamingConventions"},{"Message__c":"Method names should not start with capital letters"}],[{"File__c":"classes/VerifyDate.cls"},{"Rule__c":"MethodNamingConventions"},{"Message__c":"Method names should not start with capital letters"}]]';

List<Object> results = (List<Object>)JSON.deserializeUntyped(recordstring);
List<Map<String,Object>> keymap = new List<Map<String,Object>>();
for(Object obj:results)
{
    List<Object> objList = (List<Object>)obj;
    for(Object obj1 : objList)
    {
       Map<String,Object> keyStr = (Map<String,Object>)obj1;
       if(keyStr.containsKey('File__c'))
       system.debug('@@@ File__c  ==> ' + keyStr.get('File__c'));
       else if(keyStr.containsKey('Rule__c'))
       system.debug('@@@ Rule__c  ==> ' + keyStr.get('Rule__c'));
       else if(keyStr.containsKey('Message__c'))
       system.debug('@@@ Message__c  ==> ' + keyStr.get('Message__c'));
    }
}


Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Maharajan.C
Diwakar G 7Diwakar G 7

Hi Maharajan,
 

For the above json it worked. Need your help for below json. It is an array of arraylist. Don't know how to proceed.

String recordstring = '[[[{"usname":"US-002"},{"to":"Diw"}]],[[{"File__c":"classes/VerifyDate.cls"},{"Rule__c":"MethodNamingConventions"},{"Message__c":"Method names should not start with capital letters"}],[{"File__c":"classes/VerifyDate.cls"},{"Rule__c":"MethodNamingConventions"},{"Message__c":"Method names should not start with capital letters"}],[{"File__c":"classes/VerifyDate.cls"},{"Rule__c":"MethodNamingConventions"},{"Message__c":"Method names should not start with capital letters"}]]]'

Thanks and Regards,
Diwakar G

 
Maharajan CMaharajan C
Hi Diwakar,

Try the below updated one:

String recordstring = '[[[{"usname":"US-002"},{"to":"Diw"}]],[[{"File__c":"classes/VerifyDate.cls"},{"Rule__c":"MethodNamingConventions"},{"Message__c":"Method names should not start with capital letters"}],[{"File__c":"classes/VerifyDate.cls"},{"Rule__c":"MethodNamingConventions"},{"Message__c":"Method names should not start with capital letters"}],[{"File__c":"classes/VerifyDate.cls"},{"Rule__c":"MethodNamingConventions"},{"Message__c":"Method names should not start with capital letters"}]]]';

List<Object> results = (List<Object>)JSON.deserializeUntyped(recordstring);
Map<String,Object> keymap = new Map<String,Object>();
List<Object> objList2 = new List<Object>();
for(Object obj:results)
{
    List<Object> objList = (List<Object>)obj;
    for(Object obj1 : objList)
    {
        objList2.add(obj1);
    }
}

for(Object obj2:objList2 )
{
    List<Object> objList1 = (List<Object>)obj2;
    
    for(Object obj3:objList1)
    {
        Map<String,Object> keyStr = (Map<String,Object>)obj3;
        if(keyStr.containsKey('usname'))
            system.debug('@@@ usname  ==> ' + keyStr.get('usname')); 
        else if(keyStr.containsKey('to'))
            system.debug('@@@ to  ==> ' + keyStr.get('to'));
        else if(keyStr.containsKey('File__c'))
            system.debug('@@@ File__c  ==> ' + keyStr.get('File__c'));
        else if(keyStr.containsKey('Rule__c'))
            system.debug('@@@ Rule__c  ==> ' + keyStr.get('Rule__c'));
        else if(keyStr.containsKey('Message__c'))
            system.debug('@@@ Message__c  ==> ' + keyStr.get('Message__c'));    
    }
    
}

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Maharajan.C
This was selected as the best answer
Abdul KhatriAbdul Khatri
Hi Diwakar

I thought you have a relation between File, Rule and Message, so I went through a specific class route. If that is not the case, I think then Maharjan code looks fine you can go with that.
Diwakar G 7Diwakar G 7
Thank you Maharajan and Abdul.