Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX (Archived)

Consume JSON based web API in AX 2012

Posted on by 40

Hi All,

We have a requirement to consume JSON based Web API in AX 2012 R3. I am new to this requirement and with the help of some posts I tried to use the below sample code by executing in a Job in X++.

RetailWebRequest    request; 
RetailWebResponse   response;
    str                 rawResponse;
    Map                 responseData;
    str                 responseValue;
   
    RetailCommonWebAPI webApi = RetailCommonWebAPI::construct(); 
    request = RetailWebRequest::newUrl("echo.jsontest.com/.../Clark");
    response = webApi.getResponse(request); 
    rawResponse = response.parmData(); 
    responseData = RetailCommonWebAPI::getMapFromJsonString(rawResponse);
    responseValue = responseData.lookup("Kent");
     info(strFmt("Element name: %1", responseValue));

However, I could not get any response. Can anyone please help me on this?  

Regards

Ana

*This post is locked for comments

  • RE: Consume JSON based web API in AX 2012

    Hi Martin,

    I found why it was not working. I couldn't get the web response due to connectivity issues while accessing the URL. Now, I am able to get the response value.

    And I need to check with the URL provided by the Customer which is a different URL. Will let you know if I face any issues.

    Regards

    Ana

  • Martin Dráb Profile Picture
    Martin Dráb 230,198 Most Valuable Professional on at
    RE: Consume JSON based web API in AX 2012

    What do you mean by "not executing"? That you call the method, it executes but it gives you an empty result?

    If you get the right data in rawResponse but nothing from getMapFromJsonString(), the bug is in using getMapFromJsonString(). It's not a general-purpose deserializer; I think it can handle only a certain structure.

    You can using Newtonsoft.Json library directly to deserialize JSON. Create a C# class library in AOT, use "Paste JSON as classes" to create classes to deserialize to and use Newtonsoft.Json to do the job. Then you can use these classes in your code, which is even easier than working with maps.

  • RE: Consume JSON based web API in AX 2012

    Hi Martin,

    Thanks for your reply. While debugging the code, I have observed in RetailCommonWebAPI::getMapFromJsonString method, the line CLRObject dict = ser.DeserializeObject(_jsonString); is not executing. It is not throwing any exception. I am not sure if am missing something in the code?

    In the Url, "Kent" value is available. (echo.jsontest.com/.../Clark)

    Please let me know if any other details I have to share.

    Regards

    Ana

  • Martin Dráb Profile Picture
    Martin Dráb 230,198 Most Valuable Professional on at
    RE: Consume JSON based web API in AX 2012

    Can you elaborate what exactly happened? Did it throw an exception? If so, where? Did parmData() return nothing? Or did the serialization fail? Or the object didn't have "Kent" property?

    We can't help you unless you collect more information.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans