JSON utility class for AX 2012

Continue reading

When implementing Retail for Dynamics AX you could have a lot of different retail channels and with it a lot of different messages that need to communicate with Dynamics AX.

One possibility is to send JSON messages to Microsoft Dynamics AX which is a lightweight data-interchange format. First rule is the table field that will store the received JSON message should be of type ‘String’ and fixed character length of 1999 which we can add it as a new field in ‘RetailTransactionTable’. In this scenario we will receive a JSON message with payment information for particular Sales order. So let’s go step by step:

Step 1

The first step is to create a class called ‘JsonHelper’ and add the main parsing method as follows:

static Array parseJson(str  _json)


   container       con;

   int             jsonSubstrNo, i;

   TextBuffer      textBuffer;

   Array           returnArray = new Array(Types::Class);



   // extract the json substrings and add them to a Array

   textBuffer = new TextBuffer();


   while (textBuffer.nextToken(0, #LBracket+#RBracket))


       if ((jsonSubstrNo mod 2) > 0)

           con += #LBracket + textBuffer.token() + #RBracket;



   for (i = 1; i <= conLen(con); i++)


       returnArray.value(i, RetailCommonWebAPI::getMapFromJsonString(conPeek(con, 1)));


   return returnArray;


The above method extracts the JSON values and adds them into a container which then is being converted to a map with which we are filling a result array.

Step 2

Then add the methods that will process the array information from the previous step. One sample method is:

static SalesTable jsonArrayToPaymentInfo(SalesTable _salesTable)


   Array                       jsonArray;

   MapIterator                 mi;

   Map                         data;

   int                         i;

   str                         value;


   jsonArray = JsonHelper::parseJson(_salesTable.ANG_PaymentInformation);

   for (i = 1; i <= 1/*jsonArray.lastIndex()*/; i++)


       data = jsonArray.value(i);

       mi = new MapIterator(data);

       while (mi.more())


           value = data.lookup(mi.key());

           // initialize the payment fields in sales table before the order is created

           switch (mi.key())


               case #CreatedDate:

                   _salesTable.ANG_Payment_CreatedDate = JsonHelper::convertJsonDateToUtc(value);


               case #Details:

                   _salesTable.ANG_Payment_Details = value;


               case #IsPaid:

                   _salesTable.ANG_Payment_isPaid = !value? NoYes::No : NoYes::Yes;


               case #MBEntity:

                   _salesTable.ANG_Payment_MBEntity = value;


               case #Mode:

                   _salesTable.ANG_Payment_Mode = value;


               case #PayDate:

                   _salesTable.ANG_Payment_PayDate = JsonHelper::convertJsonDateToUtc(value);


               case #Reference:

                   _salesTable.ANG_Payment_Reference = value;






   return _salesTable;


So in the above code we have used a macro ‘ANG_JsonAttributeDef’ which holds the definitions of the attributes that we should decode. It should look like following:

//Define the json attributes for payment information








Step 3

In the previous step we have used also additional method called ‘convertJsonDateToUtc’ which is used for converting the time from JSON message into an AX date time value. So this is the method:

static utcDateTime convertJsonDateToUtc(str _jsonDateTime)


   utcDateTime resultDateTime, unixStartTime;

   str         jsonDateTimeTmp, jsonTimeZoneTmp;

   jsonDateTimeTmp = subStr(_jsonDateTime, strFind(_jsonDateTime,'-',1,strLen(_jsonDateTime))-1, -(strFind(_jsonDateTime,'-',1,strLen(_jsonDateTime))-7));

   jsonTimeZoneTmp = subStr(_jsonDateTime, strFind(_jsonDateTime,'-',1,strLen(_jsonDateTime))+1,4);

   unixStartTime = DateTimeUtil::newDateTime(mkDate(1,1,1970),0,Timezone::GMT_DUBLIN_EDINBURGH_LISBON_LONDON);

   resultDateTime  = DateTimeUtil::addSeconds(unixStartTime, str2int64(jsonDateTimeTmp)/1000);

   resultDateTime  = DateTimeUtil::addHours(resultDateTime, str2int(subStr(jsonTimeZoneTmp, 1, 2)));

   return  resultDateTime;


Step 4

And the usage of this class is shown in the following code which is inserted into ‘CreateOrder’ method in the ‘RetailOrderManager’ class:

//parse the json for payment information and initialize the corresponding fields

           salesTable = ISL_JsonHelper::jsonArrayToPaymentInfo(salesTable);

And ‘RetailOrderManager’ class is used to create a sales order and related information from a given Retail transaction table entry. And Retail transaction table gets filled by the P-job which is asynchronous way of pulling POS data to HQ.

And so that’s all for this solution that shows that Microsoft Dynamics AX could be integrated with almost any external system.

Happy DAXing.

More articles
Explore our blog

What can we do for you?

We'd love to hear about your project. Our team will get back to you within two working days.

Thank you for inquiry, we’ve passed it to our sales department, our representative will reach you back in his earliest convenience.
Oops! Something went wrong while submitting the form.