Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Answered

Receiving Json Payload into Business Central via CodeUnit Parameter

(0) ShareShare
ReportReport
Posted on by 82

Hi fellow developers,

I am looking for a way to receive and process JSON data posted into Business Central via external API.

After some research I found the easiest way to achieve this is to write a CodeUnit with parameter to get the Json payload. Below is my Codeunit:

codeunit 50220 "Process Json Data"
{
    procedure ProcessJson(orderReference: Text; orderDetails: BigText): Text
    begin
        //code to process JSON
    end;
}
and the json content I want to process:
{
    "orderReference": "PO123456789",
    "orderDetails": [
        {
            "manufactureDate": "2022-04-09",
            "itemCode": "item1",
            "quantity": 20,
        },
        {
            "manufactureDate": "2022-04-09",
            "itemCode": "item2",
            "quantity": 20,
        }
    ]
}

So far, I tried many data types for orderDetails(JsonObject, Text, BigText...), but I still could not get the "orderDetails" into the CodeUnit parameter and I kept getting 400 or 404 errors.

Can someone please help me with receiving the above JSON body with codeunit parameter? If anyone has better solutions, please let me know too. Thanks in advance!

  • JV-15051434-0 Profile Picture
    JV-15051434-0 16 on at
    Receiving Json Payload into Business Central via CodeUnit Parameter
    It is annoying that we cannot just recieve a httpbody payload. It would be great if it could. please vote for this:
  • Hasnain Ali Profile Picture
    Hasnain Ali 132 on at
    RE: Receiving Json Payload into Business Central via CodeUnit Parameter

    Hi @Ginger Frog,

    I've the same issue, receiving JSON as a parameter.

    getting same errors when sending POST request e.g., 400, 404

    Did you had any luck reading the Object?

    if we set "orderDetails" as "BigText" then we have to send the body as:

    {
    "orderReference": "PO123456789",
    "orderDetails": "[{"manufactureDate": "2022-04-09","itemCode": "item1","quantity": 20,}]
    }

    that is not a valid JSON body and other system is sending the response as a valid JSON.

    Any help will be appreciated

  • Ginger Frog Profile Picture
    Ginger Frog 82 on at
    RE: Receiving Json Payload into Business Central via CodeUnit Parameter

    Hi everyone.

    To close the loop here, I found a way forward.

    I first need to turn the orderDetails Json into a long string via Power platform because there is apparently there is no way for us to directly input full Json object via Web Service.

    pastedimage1669219872985v1.png

    Then in AL I did the below to extract the data:

    procedure ProcessJson(orderReference: Text; orderDetails: Text): Text
    var
        orderDetailsJsonArray: JsonArray;
    begin
        if not orderDetailsJsonArray.ReadFrom(orderDetails) then begin
            Error('Invalid Json body');
        end else begin
            //code to process the extracted json array orderDetailsJsonArray
        end;
    end;
  • Suggested answer
    DAnny3211 Profile Picture
    DAnny3211 9,274 Moderator on at
    RE: Receiving Json Payload into Business Central via CodeUnit Parameter

    hi

    yes,  ATC_JTM_PatternInterfaceJerome is from an external extension

    DAniele

  • Ginger Frog Profile Picture
    Ginger Frog 82 on at
    RE: Receiving Json Payload into Business Central via CodeUnit Parameter

    Hi Daniele,

    Thanks a lot. I see you are turning the entire Json body into a String and parse the parameter in as Text and it works.

    Can I ask if ATC_JTM_PatternInterfaceJerome you used in your code to convert Stringified Json back to Json is from an external extension? I cannot seem to find it in my AL. I am on Cloud v21.0.

    The Page API would be an option too! I might end up using it if I really can't get this Json thing working with my Codeunit :(. My Json payload is complicated with multiple hierarchy so creating Page API means I will have to design the page with many sub page part and muti level of deep insert. I've been avoid doing it.

    Thank you,

    Ginger

  • Verified answer
    DAnny3211 Profile Picture
    DAnny3211 9,274 Moderator on at
    RE: Receiving Json Payload into Business Central via CodeUnit Parameter

    hello

    you expose a Codeunit with an incoming parameter containing a base64 of the Json, and once received you parse it on bc

    pastedimage1669141450437v1.png

    I call up this WS from a logc app like this, see the link below from my blog:

    https://danieleincalza.blogspot.com/2022/10/oauth20-calling-custom-ws-business.html

    I would also recommend another solution, to create a Page APi with the structure of your json, so that the post call inserts the data directly into your table

    https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/devenv-api-pagetype

    check my answer if it helped you, thanks

    DAniele

  • Ginger Frog Profile Picture
    Ginger Frog 82 on at
    RE: Receiving Json Payload into Business Central via CodeUnit Parameter

    Hi ZHU,

    Thank you for linking the tutorial. My situation is a little different here because our integration partner doesn't have a server for us to run the GET call.

    Instead, our partner can only POST to us, so I think the only way of getting the JSON payload into Business Central is by creating a web service for a CodeUnit and get the JSON into the system with the JSON being my CodeUnit's parameter.

    I saw in Erik's example; he created a procedure like below with Json object as parameter:

    pastedimage1669137845661v1.png

    I've tried the same, but it doesn't work if the procedure is being called from a Web Service. It only works if the Procedure is being called within the AL code.

    Thank you,

    Ginger

  • Suggested answer
    YUN ZHU Profile Picture
    YUN ZHU 77,925 Super User 2025 Season 1 on at
    RE: Receiving Json Payload into Business Central via CodeUnit Parameter

    Hi, There is a good example in Erik's video below, I hope it can give you some help.

    How to make a simple API call with a json reply in Business Central

    https://www.youtube.com/watch?v=D5jzrgl1EP0

    pastedimage1669075146945v1.png

    Hope this helps.

    Thanks.

    ZHU

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,494 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,305 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans