Pages

Thursday, January 1, 2015

Testing JSON from AX 2012

I wanted to see how JSON could be consumed from AX, and found this great article from Jonathan.

Then I needed something to test with and found the site jsontest.com.

And so, here are two examples.

The first example just returns your IP address:
static void JSONTestIP(Args _args)
{ 
    RetailWebRequest    request; 
    RetailWebResponse   response; 
    str                 rawResponse; 
    Map                 responseData; 
    str                 responseValue;
    
    RetailCommonWebAPI webApi = RetailCommonWebAPI::construct(); 
    
    request = RetailWebRequest::newUrl("http://ip.jsontest.com"); 
    response = webApi.getResponse(request); 
    
    rawResponse = response.parmData(); 
    
    responseData = RetailCommonWebAPI::getMapFromJsonString(rawResponse); 
    responseValue = responseData.lookup("ip");
 
    info(strFmt("Element name: %1", responseValue));
}

The next example returns key/value pairs. In this example first name of a person is found based on the persons last name:
static void JSONTestKeyValue(Args _args)
{ 
    RetailWebRequest    request; 
    RetailWebResponse   response; 
    str                 rawResponse; 
    Map                 responseData; 
    str                 responseValue;
    
    RetailCommonWebAPI webApi = RetailCommonWebAPI::construct(); 
    
    request = RetailWebRequest::newUrl("http://echo.jsontest.com/Agermark/Palle/Kent/Clark"); 
    response = webApi.getResponse(request); 
    
    rawResponse = response.parmData(); 
    
    responseData = RetailCommonWebAPI::getMapFromJsonString(rawResponse); 
    responseValue = responseData.lookup("Kent");
 
    info(strFmt("Element name: %1", responseValue));
}

5 comments:

Sukrut said...

Do you have any example where we can convert map to json string . If you know any standard ax method does that please let me know ..Thanks

Sukrut said...

if you know if any method in AX converts map to json string please let me know .Thanks

Kolkata Guy said...

Hi Sukrut,

Were you able to find something to convert MAP to JSON.

Thanks..

Kolkata Guy said...

Hi Sukrut,

Did you find anyway to convert map to json.

Thanks...

Melvin Steenkamp said...

Hi,

Do you know how to convert from map or xml to json?


Regards