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
VSK98VSK98 

Getting Null value in JSON

Hi All,

I am working JSON Parsing which is very new to me. I am getting the response and deserialized by using below method.
Map<String, Object> deSer = (Map<String, Object>) JSON.deserializeUntyped(resp.getbody());
Response Body is below:
{
	value = ({
		group_name =  *******,
		groupid = 1,
		sub_customer = [{
			"CustomerId": "9488490",
			"CustomerName": "*******",
			"Street": "*******,"
			City ":"
			KINGSWOOD ","
			PostalCode ":"
			******* ","
			Country ": Australia",
			"Assetcnt": "0"
		}, {
			"CustomerId": "9488491",
			"CustomerName": "*******",
			"Street": "*******,"
			City ":"
			PENRITH ","
			PostalCode ":"
			******* ","
			Country ": Australia",
			"Assetcnt": "0"
		}, {
			"CustomerId": "94106344",
			"CustomerName": "*******",
			"Street": "*******,"
			City ":"
			KINGSWOOD ","
			PostalCode ":"
			******* ","
			Country ": Australia",
			"Assetcnt": "0"
		}, {
			"CustomerId": "94126502",
			"CustomerName": "*******",
			"Street": "DERBY STREET,"
			City ":"
			KINGSWOOD ","
			PostalCode ":"
			******* ","
			Country ": Australia",
			"Assetcnt": "0"
		}, {
			"CustomerId": "94106486",
			"CustomerName": "*******",
			"Street": "*******,"
			City ":"
			KINGSWOOD ","
			PostalCode ":"
			******* ","
			Country ": Australia",
			"Assetcnt": "0"
		}, {
			"CustomerId": "9488489",
			"CustomerName": "*******",
			"Street": "*******,"
			City ":"
			PENRITH ","
			PostalCode ":"
			******* ","
			Country ": Australia",
			"Assetcnt": "0"
		}]
	})
}

I would like to get a list of Sub_Customers by using below method which is getting NULL Values.
 
List<Object> lstObj = (List<Object>) deSer.get('sub_customer');

Once I get the list<Object> I need to pass to Lightning: Datatable to display as a tree Grid view.

Regards,
VSK98
 
Raj VakatiRaj Vakati
You need to go two levels down .. 

Mean first get the value  
and convert it to map 
from there get thr sub_customer
karthikeyan perumalkarthikeyan perumal
Hello, 

I request you to validate the format of JSON DATA.  i think you have formed the format wrongly. 

copy your JSON data  and paste here then click validate,  if your corretc structure of JSON Data. its easy to get the value using this code. 
 
List<Object> lstObj = (List<Object>) deSer.get('sub_customer');
as per my understanding your JSON data FOrmat is wrong.  For Example 

"Street": "*******,"   
City ":" KINGSWOOD ",
" PostalCode ":" ******* ",
" Country ": Australia",
"Assetcnt": "0"

"Street": "*******,"-- See the comma where its placed its after double qts.  



value = ({ group_name = *******, groupid = 1, sub_customer =

Use  : insted of =  values should be in side qts.

use this URL. 

https://codebeautify.org/jsonviewer

Hope this will give you some idea. 

Thanks
karthik
 
VSK98VSK98
Hi Karthik.

Please find the response below. I hope response is valid .
{
	"value": [{
		"groupid": 1,
		"group_name": "*******",
		"sub_customer": "[{\"CustomerId\":\"9488490\",\"CustomerName\":\"*******\",\"Street\":\"Derby Street,\"City\":\"KINGSWOOD\",\"PostalCode\":\"2750\",\"Country\": Australia\",\"Assetcnt\":\"0\"},{\"CustomerId\":\"9488491\",\"CustomerName\":\"*******\",\"Street\":\"Great Western Highway,\"City\":\"PENRITH\",\"PostalCode\":\"2750\",\"Country\": Australia\",\"Assetcnt\":\"0\"},{\"CustomerId\":\"94106344\",\"CustomerName\":\"*******\",\"Street\":\"Unit 11, 83 Cox Avenue,\"City\":\"KINGSWOOD\",\"PostalCode\":\"2747\",\"Country\": Australia\",\"Assetcnt\":\"0\"},{\"CustomerId\":\"94126502\",\"CustomerName\":\"*******\",\"Street\":\"DERBY STREET,\"City\":\"KINGSWOOD\",\"PostalCode\":\"2747\",\"Country\": Australia\",\"Assetcnt\":\"0\"},{\"CustomerId\":\"94106486\",\"CustomerName\":\"*******\",\"Street\":\"SOMERSET STREET,\"City\":\"KINGSWOOD\",\"PostalCode\":\"2747\",\"Country\": Australia\",\"Assetcnt\":\"0\"},{\"CustomerId\":\"9488489\",\"CustomerName\":\"*******\",\"Street\":\"Great Western Highway,\"City\":\"PENRITH\",\"PostalCode\":\"2750\",\"Country\": Australia\",\"Assetcnt\":\"0\"}]"
	}]
}

Regards,
VSK98
karthikeyan perumalkarthikeyan perumal
Hello, 

Good work. 

kinldy use the below code to get the values of sub_customer. 
 
Map<String, Object> metaJSON = (Map<String, Object>) JSON.deserializeUntyped(JsonString);
List<Map<String, Object>> myMaps = new List<Map<String, Object>>();
List<Object> lstObj = (List<Object>) metaJSON .get('sub_customer');
for (Object obj : lstObj ) {
    myMaps.add((Map<String, Object>)obj);
}
Hope this will help to get those value. 

Thanks
karthik


 
VSK98VSK98
Hi Karthik,

I had tried the same method but I am getting NULL values...
List<Object> lstObj = (List<Object>) metaJSON .get('sub_customer');

Regards,
VSK98​​​​​​​
karthikeyan perumalkarthikeyan perumal
Hello, 

Can you post your apex class or your full code to try it from my side. 

Thaks
karthik
 
VSK98VSK98
Hi Karthik,

Thanks for the quick response !!

Apex Class:
Public class infos{
   
              
   @AuraEnabled   
 public Static void getCuslst(string search){
    
   string toUppercs = search.toUpperCase();
   string str;
   str= toUppercs.trim();   
   
   str = str.replaceAll('(\\s+)', '%20');    
   system.debug('str*****'+str);
     
    
     
     HttpRequest req = new HttpRequest(); 
     string edPnt = '**************'
     
     
     req.setMethod('GET');
     req.setEndpoint(edPnt);
     req.setTimeout(120000);   
     String username = '*****';
     String password = '*****';
   
    
    Blob headerValue = Blob.valueOf(username + ':' + password);
    req.setHeader('Content-Type', 'application/json');
    req.setHeader('Authorization', 'Basic *****');
    
           
    Http h = new Http();
    HttpResponse resp = new HttpResponse();   
    resp = h.send(req);
    
    
   
    Map<String, Object> meta = (Map<String, Object>) JSON.deserializeUntyped(resp.getbody());
   
    List<Object> myMapObj = (List<Object>) meta.get('sub_customer'); --------getting null
 
    system.debug('myMapObjects********'+myMapObj);
   
   }

    
  }

Regards,
VSK98 
karthikeyan perumalkarthikeyan perumal
Hello, 

Try this code, 
 
Public class infos{
   
              
   @AuraEnabled   
   public Static void getCuslst(string search){
    
   string toUppercs = search.toUpperCase();
   string str;
   str= toUppercs.trim();   
   
   str = str.replaceAll('(\\s+)', '%20');    
   system.debug('str*****'+str);
     
    
     
     HttpRequest req = new HttpRequest(); 
     string edPnt = '**************'
     
     
     req.setMethod('GET');
     req.setEndpoint(edPnt);
     req.setTimeout(120000);   
     String username = '*****';
     String password = '*****';
   
    
    Blob headerValue = Blob.valueOf(username + ':' + password);
    req.setHeader('Content-Type', 'application/json');
    req.setHeader('Authorization', 'Basic *****');
    
           
    Http h = new Http();
    HttpResponse resp = new HttpResponse();   
    resp = h.send(req);
    
    
    Map<String, Object> metaJSON = (Map<String, Object>) JSON.deserializeUntyped(resp.getbody());
    List<Map<String, Object>> myMaps = new List<Map<String, Object>>();
    List<Object> lstObj = (List<Object>) metaJSON .get('sub_customer');
    
    for (Object obj : lstObj ) {
    myMaps.add((Map<String, Object>)obj);
    }
    
     system.debug('myMapObjects********'+myMaps);
   
   }

    
  }

Thanks
karthik