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
mariagusmariagus 

How to deserialize a JSON file with father and child

Hi all,

 

I'm working on a development that gets a JSON file with a father and child objects information. After that, I could do some changes in the JSON file directly and after importing, I create with this new data a new father and child record. It used the old JSONObejct class and it was working fine, but I got a too many script statament error so I decide to take a look at the new JSON Winter'12 feature.

 

The current JSON.serialize(SObject) method works fine and returns me this:

 

{
    "attributes": {
        "type": "MyObject__c",
        "url": "..................................."
    },
    "Id": "a1SD00000005Hk5MAE",
    "Field1__c": "Test1-complex",
    "Field2__c": "Test2-complex",
    "MyObjectLineItems__r": {
        "size": 1,
        "totalSize": 1,
        "done": true,
        "records": [
            {
                "attributes": {
                    "type": "MyObjectLineItem__c",
                    "url": "............................................................."
                },
                "Id": "a1TD0000000FFAJMA4",
                "Field1__c": "text1",
                "Field2__c": 150,
                "Field3__c": 155.55,
                "Field4__c": true,
                "Field5__c": "2011-12-20",
                "Field6__c": "2011-12-20T15:24:00.000+0000",
                "Field7__c": "No",
                "MyObject__c": "a1SD00000005Hk5MAE"
            }
        ],
        "queryLocator": null
    }
}

 

But I don't know how to use the deserialize method because it's second argument is the type, and here I have two objects.

 

I tried to create my own deseriaze method, using the JSON.createParser(JSONString); but, in order to make my code as easier as possible, I wanted to use the parser.readValueAs() method. But I'm not sure how to do it because, I don't have in the JSON string the name of my father object, and if I try to do it with my child one, it doesn't work either.

 

This is my code:

 

List<MyObject__c> myObjectList = [Select Id,
                                                                             Field1__c, //Text
                                                                             Field2__c, //Text
                                                                            (Select Id, 
                                                                                          Field1__c, //Text
                                                                                          Field2__c, //Integer
                                                                                          Field3__c, //Decimal
                                                                                          Field4__c, //Checkbox
                                                                                          Field5__c, //Date
                                                                                          Field6__c, //DateTime
                                                                                          Field7__c, //Picklist
                                                                                          MyObject__c
                                                                             From MyObjectLineItems__r)               
                                                             From MyObject__c
                                                             Where Id = :myObjectId];
   
   if(myObjectList.isEmpty())
   {
               throw new MyJSONException('There is no object which ID is ' + myObjectId);   
   }
   else
   {
                String JSONString = JSON.serialize(myObjectList.get(0));
               

                // Parse JSON response
               JSONParser parser = JSON.createParser(JSONString);
    
               while(parser.nextToken() != null)
               {
                      if(parser.getCurrentToken() == System.JSONToken.FIELD_NAME)
                      {
                              if(parser.getText() == 'MyObjectLineItems__r')
                              {
                                             Type lineItem = Type.forName('MyObjectLineItem__c');
                                             MyObjectLineItem__c so = (MyObjectLineItem__c)parser.readValueAs(lineItem);       
                              }
                       }
                }
   }

 

I know that I could go throw each line, and read the field and the value, but I would like to do as quicker as possible.

 

Can someone help me?

 

 

Many thanks in advance.

 

Best Answer chosen by Admin (Salesforce Developers) 
mariagusmariagus

 

My previous code worked. But I had to something more. Include this code into a inner class, and call this inner class in the serialize and deserilaize method:

 

public with sharing class MyObject
    {
        public String Field1 {get; set;}
        public String Field2 {get; set;}
        public List<MyObjectLineItem> LineItemList {get; set;}

        public MyObject(String f1, String f2)
        {
            Field1 = f1;
            Field2 = f2;
            LineItemList = new List<MyObjectLineItem>();
        }
        public class MyObjectLineItem
        {
            public String Field1 {get; set;}
            public String Field2 {get; set;}
            public MyObjectLineItem (String f1, String f2)
            {
                Field1 = f1;
                Field2 = f2;
            }
        }
    }

 

 

 

All Answers

Henry AkpalaHenry Akpala

Hi,

to use deserialize, you need to create a class that has the structure of the data that you are trying to desierialize.  then all you do is pass the data to the class using the JSON syntax.  There is a very good example on this site, see url below.

 

http://www.tgerm.com/2011/10/winter12-jsonparser-serialize.html

 

Hope this helps.

Regard

HenryAG

mariagusmariagus

Many thanks, your link was quite useful to understand how to work with JSON properly.

 

But when I try to do somthing similar, I can't compile:

 

    public class MyObject
    {
        public String Field1 {get; set;}
        public String Field2 {get; set;}
        public List<MyObjectLineItem> LineItemList {get; set;}
        
        public MyObject(String f1, String f2)
        {
            Field1 = f1;
            Field2 = f2;
            LineItemList = new List<MyObjectLineItem>();
        }
        
        public MyObject(String f1, String f2, List<MyObjectLineItem> lItemList)
        {
            Field1 = f1;
            Field2 = f2;
            LineItemList = lItemList;
        }
    }

 

    public class MyObjectLineItem
    {

         ..............

    }

 

    public void deserialize(String JSONString)
    {
            MyObject deserialize = (MyObject)JSON.deserialize(JSONString, MyObject.class);
    }

 

If I changet it to this:

 

    public void deserialize(String JSONString)
    {

            Type myType = Type.forName('MyObject');

            MyObject deserialize = (MyObject)JSON.deserialize(JSONString, myType);
    }

 

It compile, but the run process ruturns this error: "Don't know the type of the Apex object to deserialize at [.......]"

 

Any idea?

 

Thanks

mariagusmariagus

Thanks a lot. I just read few days ago the solution but not all thread.

 

I will use the JSONParse class and move to deserialized method once we have Sprint'12 released.

mariagusmariagus

 

My previous code worked. But I had to something more. Include this code into a inner class, and call this inner class in the serialize and deserilaize method:

 

public with sharing class MyObject
    {
        public String Field1 {get; set;}
        public String Field2 {get; set;}
        public List<MyObjectLineItem> LineItemList {get; set;}

        public MyObject(String f1, String f2)
        {
            Field1 = f1;
            Field2 = f2;
            LineItemList = new List<MyObjectLineItem>();
        }
        public class MyObjectLineItem
        {
            public String Field1 {get; set;}
            public String Field2 {get; set;}
            public MyObjectLineItem (String f1, String f2)
            {
                Field1 = f1;
                Field2 = f2;
            }
        }
    }

 

 

 

This was selected as the best answer