web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :

Constructing JSON String with array of objects in D365FO

Chaitanya Golla Profile Picture Chaitanya Golla 17,225

Hi,

In this post, we will create a JSON string that can have an array of objects. Table CustTable is chosen from which data is extracted and its contact information based on type is shown as different objects in the array.

class CG_BuildJSONWithObjectsOfArray
{       
    /// <summary>
    /// Runs the class with the specified arguments.
    /// </summary>
    /// <param name = "_args">The specified arguments.</param>
    public static void main(Args _args)
    {   
        System.IO.StringWriter          stringWriter;
        Newtonsoft.Json.JsonTextWriter  jsonWriter;
        CustTable                       custTable;
        LogisticsElectronicAddress      address;
        DirPartyLocation                dirPartyLocation;
        LogisticsLocation               location;
        DirPartyTable                   dirPartyTable;
        LogisticsLocationRole           locationRole;
        LogisticsElectronicAddressRole  addressLocationRole;
 
        stringWriter       = new System.IO.StringWriter();
        jsonWriter         = new Newtonsoft.Json.JsonTextWriter(stringWriter);
 
        select firstonly AccountNum, BankAccount, Party
            from custTable
                where custTable.AccountNum == "1000"
                join dirPartyTable
            where dirPartyTable.RecId == custTable.Party;
 
        str sJSON = "";
        jsonWriter.WriteStartObject();
        jsonWriter.WritePropertyName("AccountNum");
        jsonWriter.WriteValue(custTable.AccountNum);        
        jsonWriter.WritePropertyName("Name");
        jsonWriter.WriteValue(custTable.nameAlias());        
        jsonWriter.WritePropertyName("BankAccount");
        jsonWriter.WriteValue(custTable.BankAccount);
       
        jsonWriter.WritePropertyName("Contact Information");       
        jsonWriter.WriteStartArray();
        while select Location, RecId from dirPartyLocation
            where dirPartyLocation.Party == dirPartyTable.RecId
            join RecId from location
                where location.RecId == dirPartyLocation.Location
                join address
                    where address.Location == location.RecId
                    join RecId from addressLocationRole
                            where (addressLocationRole.ElectronicAddress == address.RecId)
                            join Name from locationRole
                                where ((addressLocationRole.LocationRole == locationRole.RecId)
                                    && (locationRole.IsContactInfo == NoYes::Yes))
        {
            jsonWriter.WriteStartObject();
 
            jsonWriter.WritePropertyName("Type");
            jsonWriter.WriteValue(enum2Str(address.Type));
 
            jsonWriter.WritePropertyName("Description");
            jsonWriter.WriteValue(address.Description);
 
            jsonWriter.WritePropertyName("Value");
            jsonWriter.WriteValue(address.Locator);
           
            jsonWriter.WriteEndObject();
 
        }
        jsonWriter.WriteEndArray();
        jsonWriter.WriteEndObject();
 
        sJSON = stringWriter.ToString();
        Info(strFmt("%1", stringWriter.ToString()));
    }
 
}

Output: https://drive.google.com/open?id=1FSw9m_4C71pO7ynPsvph_l5IzK_YeOYV

Regards,

Chaitanya Golla

Comments

*This post is locked for comments