Today, I will share the code snippet which can be used to create a json file in AX 2009.
We will be using .NET Newtonsoft.Json.dll file to proceed with this work.
Download the Newtonsoft.Json.dll and place it on this path : D:\Program Files (x86)\Microsoft Dynamics AX\50\Client\Bin.
Below is the code snippet to create JSON file.
static void DataExtractToJSON(Args _args)
{
Dialog dialog;
FilePath filepath;
DialogField dialogFilePath;
InventSum inventSum;
InventDim inventDim;
WMSLocation wmsLocation;
InventTable inventTable;
InventDimParm invDimParm;
Qty availPhys;
str lpnnumber, lpnconstant, filename;
int lpnCounter, lpnLen;
date consumptionPriorityDate,expirationDate,manufacturedDate;
TextIO file;
FileIOPermission fileIOPermission;
container line,header,header2;
Newtonsoft.Json.Linq.JTokenWriter writer;
Newtonsoft.Json.Linq.JObject jObject;
ClrObject clrObject;
str jsonStr;
;
#File
dialog = new dialog();
dialog.caption("Select the folder to save JSON file");
dialogFilePath = dialog.addField(typeId(FilePath));
dialogFilePath.label('FilePath');
dialog.run();//execute dialog
filepath = dialogFilePath.value();//return path file value
if (filepath)
{
writer = new Newtonsoft.Json.Linq.JTokenWriter();
writer.WriteStartObject();
writer.WritePropertyName("Items");
writer.WriteStartArray();
fileName = FilePath+'\\test.json';
new FileIOPermission(Filename,'w').assert();
file = new TextIO(Filename,#io_write,1250);
line = connull();
while select inventLocationId, wmsLocationId,inventLocationId,locationType from wmsLocation
join inventSum
join inventDim
join inventTable
where inventDim.inventDimId == inventSUm.InventDimId
&& inventSum.ItemId == inventTable.ItemId
&& inventDim.InventLocationId == wmsLocation.inventLocationId
&& inventDim.wMSLocationId == wmsLocation.wMSLocationId
&& inventSum.Closed == NoYes::No
&& wmsLocation.checkText == ''
&& wmsLocation.locationType == WMSLocationType::Pick
{
line = connull();
availPhys = InventSum.availPhysical();
if ( availPhys != 0)
{
writer.WriteStartObject();
writer.WritePropertyName('testLocation');
writer.WriteValue(wmsLocation.wmsLocationId);
writer.WritePropertyName('TestType');
writer.WriteValue('LOCATION');
writer.WritePropertyName('TestTransactionType');
writer.WriteValue('INVENTORY_ADJUSTMENT');
writer.WritePropertyName('TestItemId');
writer.WriteValue(inventTable.ItemId);
writer.WritePropertyName('TestQty');
writer.WriteValue(availPhys);
writer.WriteEndObject();
}
}
writer.WriteEndArray();
writer.WriteEndObject();
clrObject = writer.get_Token();
jObject = clrObject;
jsonStr = jObject.ToString();
file.write(jsonStr);
info(jObject.ToString());
if(dialog.closedOk())
{
info(strfmt("Please check the JSON file on this path %1", filename));
}
}
}
Output :