Using JSON in Dynamics AX

Repost from: http://workflowax.wordpress.com/2013/10/22/using-json-in-dynamics-ax/

I’ve recently had a requirement to integrate an external system (Toggl www.toggl.com) with Dynamics AX 2012’s Timesheet system. However the external system only provided a JSON API which isn’t supported natively by the Dynamics AX AIF system.  After some research and trial and error (using various DLL’s) I eventually stumbled across some really useful classes provided by the Retail module in Dynamics AX2012. These classes (although named “Retail…”) provide a really easy and elegant JSON interaction for any use. Here is a very basic code sample of how to consume JSON using these classes in Dynamics AX 2012.

 

static void myJob(Args _args)
{ 
    RetailWebRequest request; 
    RetailWebResponse response; 
    str rawResponse, value; 
    Map data; 
    RetailCommonWebAPI webApi = RetailCommonWebAPI::construct(); 
    request = RetailWebRequest::newUrl("http://mysite.com/jsonaction"); 
    response = webApi.getResponse(request); 
    rawResponse = response.parmData(); 
    data = RetailCommonWebAPI::getMapFromJsonString(rawResponse); 
    value = data.lookup("elementname"); 
    info(strFmt("Element name: %1",value)); 
}

If you are interested in some more advanced examples of using JSON in AX (e.g. using authentication, retrieving subsets of info or arrays)  or doing some of your own integration into Toggl timekeeping please let me know and I’ll post some more info.

 

8 thoughts on “Using JSON in Dynamics AX

  1. Hi jonathan,

    I tried using RetailCommonWebAPI::getMapFromJsonString(rawResponse) but received the following error. It worked when I test it in AX Jobs but not when I tried to call it from an AX WCF method. It says to implement IConvertible. how? And how come it works in AX jobs? Thanks.

    System.InvalidCastException: Object must implement IConvertible.
    at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
    at Dynamics.Ax.Application.RetailCommonWebAPI.getMap(Object _dict) in RetailCommonWebAPI.getMap.xpp:line 11
    at Dynamics.Ax.Application.RetailCommonWebAPI.getMapFromJsonString(String _jsonString) in RetailCommonWebAPI.getMapFromJsonString.xpp:line 25
    at Dynamics.Ax.Application.drscl_JSONjsCCMReceipt.Createreceipt() in drscl_JSONjsCCMReceipt.CreateReceipt.xpp:line 31

  2. I am doing ax integration and i want to post dynamic comments to another application . i am using maps .do you know if there is any way/method in ax which converts maps to json string .

  3. Hi Jonathan,
    I am getting the same error like Dave faced.
    My Sample Json is ‘{“Name”:”123″}’.

    Even a simple Json is not parsing when called using sysoperationframweork. But works well in normal class and ax jobs,

    Any thoughts?

    Thanks in advance

    • Hi,

      I am also facing same error in batch mode for RetailCommonWebAPI.
      in non-batch processing , same class works fine.

      Kindly share the solution.

      Thanks
      Neetu

  4. I have to do integration of json with ax .

    For that I have files in jason format , I am not able to read that format in AX.

    I have created table1 with field of type “memo”. My json code is in the memo field. I have to read that Json file and create a sales order.

Leave a Reply to Sukrut Cancel reply

Your email address will not be published. Required fields are marked *