+ Start a Discussion
sf_evolutionsf_evolution 

JASON Parsing help -

Hi Everybody,

I'm missing something for a task that requires me to deserialize a JSON string. The string defines business types for many industries... 

 

And every parent insustry can have up to four (or maybe more) groups  of child sub-business types.

The basic format od the JSON is like this:

 

[
  {
    "tag_name": "Agriculture & Forestry", 
    "parent_tag_id": null, 
    "tag_id": 1, 
    "children": [
      {
        "tag_name": "Agricultural Services", 
        "parent_tag_id": 1, 
        "tag_id": 2, 
        "children": [
          {
            "tag_name": "Cotton Gins", 
            "parent_tag_id": 2, 
            "tag_id": 3
          }, 
          {
            "tag_name": "Crop Maintenance", 
            "parent_tag_id": 2, 
            "tag_id": 4
          }, 
...

    ]
  }, 
  {
    "tag_name": "Attorneys & Legal Services", 
    "parent_tag_id": null, 
    "tag_id": 204, 
    "children": [
      {
        "tag_name": "Attorneys", 
        "parent_tag_id": 204, 
        "tag_id": 205, 
        "children": [
          {
            "tag_name": "Anti-Trust Attorneys", 
            "parent_tag_id": 205, 
            "tag_id": 206
          }, 

 Etc. Etc...

In my code I defined the structure of the json hierarchy, and I'm reading the .json page OK, but I can't quite get the syntax of the
deserialize statement;

 

public class CategoryUtil {
     /*
      *   Define sub-class
      */  
     public with sharing class Cat_Tree {
        public String tag_name {get; set;}
        public integer parent_tag_id {get; set;}
        public integer tag_id {get; set;}
        
        public List<Cat_TreeChildren> TreeChildren {get; set;}

        public Cat_Tree(String f1, integer f2, integer f3) {
            tag_name = f1;
            parent_tag_id = f2;
            tag_id = f3;
            TreeChildren = new List<Cat_TreeChildren>();
            }                  
       }         
      public class Cat_TreeChildren {
            public String tag_name {get; set;}
            public integer parent_tag_id {get; set;}
            public integer tag_id {get; set;}
        
            public Cat_TreeChildren (String f1, integer f2, integer f3) {
               tag_name = f1;
               parent_tag_id = f2;
               tag_id = f3;
               }
        }        
        
    public CategoryUtil() {        
        getParameters();
        }
   
    public void getParameters () {
        Http httpProtocol = new Http();
        HttpRequest request = new HttpRequest();
        String endpoint = 'http://Externalbusiness.com/tags_hierarchy.json';

        request.setEndPoint(endpoint); 
        request.setMethod('GET');
   
        request.setHeader('Content-Type', 'application/json');
        HttpResponse response = httpProtocol.send(request);
        String strResponse = response.getBody();
        System.debug('HTTP strResponse: ' + strResponse);
        Type myType = Type.forName('Cat_Tree');
        
        //listCategories  =   Json.deserialize(strResponse, myType );    
        Cat_Tree cat_deserialize = (Cat_Tree)JSON.deserialize(strResponse , myType);
        System.debug('Cat_Tree: ' + cat_deserialize);
        }

 Basically, at the 

System.debug('HTTP strResponse: ' + strResponse);

Line I'm printing out my JSON string correctly,  but the next line:

 

Cat_Tree cat_deserialize = (Cat_Tree)JSON.deserialize(strResponse , myType);

Crashes with a "Attempt to de-reference a null object " error.

 

Any idea what I'm doing wrong?   Thanks in advance for any help.

 

 

 

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
kiranmutturukiranmutturu

just try to use the below

 

Cat_Tree cat_deserialize = (Cat_Tree)JSON.deserialize(strResponse , Cat_Tree.class);

 

 

i hope did  your org have any namespace ? then forName(fullyQualifiedName) method returns null. Instead, use theforName(namespace, name) method and specify an empty string or nullfor the namespace argument.