0

I have to populate the values in a JSON structure. The values are obtained from the fields of a parent object and also the child object. I may have more than 1 child records of a parent object. how can I populate that in the below code. How can I traverse through all the child and update it in the JSON strcuture

 String JSONString=JSON.serialize(
        new Map<String, Object> { 
            'ElementA' => new Object[] {
                new Map<String, Object> {
                'ParentField1' => field1__C,
                'ParentField2' => field2__C,            
                'child' => new Object[] {
                    new Map<String, Object> {
                    'childName' => how do i get each child Name to repeat here,
                    'childfield1' => how do i get each child's field 1 to repeat here
                    }
                },
                'ParentField3' => field3__C,
                'ParentField4' => 'field4__C
                }
}
            );

Exact JSON:

{
    "Parent": [{
        "SerialNumber": "33455724",
        "EventDay": "1",
        "EventMonth": "12",
        "EventYear": "2016",
        "Child": [{
            "Child": " ",
            "Level1": " ",
            "Level2": " ",
            "Description": " ",
            "RecordType": " "
        }],
        "AccountName": "Account 001",
        "AccountNumber": "123333",
        "Address": "NY",
        "Contact": [{
            "FirstName": " ",
            "LastName": " ",
            "Phone": " ",
            "Address": " ",
            "Email": " "
        }],
        "Lot": " ",
        "Num": " "
    }]
}

JSON2APex Class:

public class Child {
    public String Child;
    public String Level1;
    public String Level2;
    public String Description;
    public String RecordType;
}

public class Parent {
    public String SerialNumber;
    public String EventDay;
    public String EventMonth;
    public String EventYear;
    public List<child> Child;
    public String AccountName;
    public String AccountNumber;
    public String Address;
    public List<Contact> Contact;
    public String Lot;
    public String Num;
}

public class Contact {
    public String FirstName;
    public String LastName;
    public String Phone;
    public String Address;
    public String Email;
}

public class JSON2Apex {
    public List<parent> parent;
}

What I intend to do:

Parent__C pr = [select id,......... where id=parent.id];

Parent p = new Parent();
p.SerialNumber = pr.serial__c;
..
..
List<Child__C> c = [select childname,Level1,Level2,Description from child__c where child.parent__c = pr.id];
List<Child> ch = new List<Child>();
For(Child__c chld : c){
ch.Level1 = chld.Level1;
..
ch.RecordType = chld.RecordType;
}
p.Child = ch;

// similarly I will get the list of contact and do
p.Contact = contactList 

List<parent> plist = new List<parent>();
plist.add(p);


JSON2Apex j = new JSON2Apex();
j.parent = plist

String JsonString = JSON.serialize(j);

Does the above code looks correct?

  • Note that it's not very grammatically correct to have "SingularKeyName": [...]. If your value is an array, the key name should be plural. So "children" is more appropriate than "child", and "contacts" is more appropriate than "contact", etc. – Adrian Larson Apr 10 '17 at 21:50
2

A simple class to illustrate this would be:

public class myExampleObject{

    public String field1;
    public String field2;
    public myChildObject[] children;

    public class myChildObject{

         public string childField1;
         public string childField2;

    }

}

Then you could simply populate as follows (may require debugging, writing ad lib here):

myExampleObject parentObject = New myExampleObject();
parentObject.field1 = 'abc';
parentObject.myChildObject theChild = New parentObject.myChildObject();
theChild.childField1 = 'ghi';

parentObject.children = New parentObject.myChildObject[]{theChild};

Then serialize:

String serialized = JSON.serialize(parentObject);

If you know the object names it could be as simple as:

public class myWrapper{

    public Account theParentAccount;
    public Contact[] accountContacts;


}

You can always take an example of your desired final JSON output as throw it into json2apex to get the class as well. A lot depends on how the JSON needs to be as a final result as to how you structure the class

  • Comments are not for extended discussion; this conversation has been moved to chat. – Adrian Larson Apr 11 '17 at 0:14
  • How to do the same in more generic way? E.g. without knowing the field names of the child? So we can't depend on the wrapper class. Any lead? – L Mudiyanse Jun 22 '18 at 3:40
2

You would add each as a separate Map<String, Object>:

'children' => new List<Object>
{
    new Map<String, Object>
    {
        'Name' => 'Child 1',
        'SomeField__c' => 'Some Value'
    },
    new Map<String, Object>
    {
        'Name' => 'Child 2',
        'SomeField__c' => 'Some Other Value'
    }
}
  • 1
    @SfdcBat - If you have an example final JSON output then throw it in json2apex and get your class. I believe you have asked questions about this before and directed there as well no? salesforce.stackexchange.com/questions/167877/… - same steps to be taken here – Eric Apr 10 '17 at 21:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.