web
You’re offline. This is a read only version of the page.
close
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...
Suggested Answer

Not able to read JSON from Code unit API in Business Central

(1) ShareShare
ReportReport
Posted on by 163
Hi All,


I have created one Code Unit and created one web service by this code unit, but i am getting error when i test this API from POSTMAN. I am not able to read the JSON, getting same issue.
Does anyone have any idea about it please help or if you code any code snip please share with me.

Code Unit : 
 
    procedure ReadJSON(JsonObjectText: Text): Text
    var
        ArrayJSONManagement: Codeunit "JSON Management";
        JSONManagement: Codeunit "JSON Management";
        ObjectJSONManagement: Codeunit "JSON Management";
        i: Integer;
        CodeText: Text;
        CustomerJsonObject: Text;
        JsonArrayText: Text;
        ShipToJsonObject: Text;
        T: Text;
    begin
        JSONManagement.InitializeObject(JsonObjectText);
        if JSONManagement.GetArrayPropertyValueAsStringByName('Customer', CustomerJsonObject) then begin
            ObjectJSONManagement.InitializeObject(CustomerJsonObject);
            ObjectJSONManagement.GetStringPropertyValueByName('No', CodeText);
            ObjectJSONManagement.GetStringPropertyValueByName('Address', T);
            exit(CodeText + ' ' + T);
        end;
    end;

API Body JSON : 
{
    "Customer": {
        "No": "10000",
        "Address": "192 Market Square"
    }
}

Error: 
 
{
    "error": {
        "code": "BadRequest",
        "message": "The parameter 'Customer' in the request payload is not a valid parameter for the operation 'CreateSO_ReadJSON'.  CorrelationId:  ff7d9870-d7ae-4245-ba7b-05efe224cf7d."
    }
}
I have the same question (0)
  • Suggested answer
    YUN ZHU Profile Picture
    95,331 Super User 2025 Season 2 on at
  • Suggested answer
    Ramiz Profile Picture
    597 on at
    Hi,

    I dont think that you can pass a json like that to a codeunit webservice. The error that you are getting is of the parameter name in the API function which is JsonObjectText and in the API body the first parameter is Customer.

    You either need to create one text string in the API body as mentioned here or convert your whole json body to a base64 text and pass it.

    I personally prefer to do it using Base64. For-example, I have this JSON and want to pass it to a codeunit API. What I do is convert it to a base64 and then send the post request.
     
    {
      "orderDate": "2024-10-10",
      "postingDate": "2024-10-10",
      "customerId": "d46301ed-970b-ee11-962f-00224859f0ad",
      "currencyCode": "SGD",
      "salesperson": "LEROY",
      "salesOrderLines": [
        {
          "itemId": "eb230e11-160c-ee11-962f-00224859f0ad",
          "lineType": "Item",
          "lineObjectNumber": "10810900019",
          "description": "AXE BODY WASH (INDIA) 250ML X 12 AFRICA",
          "unitOfMeasureId": "add2b303-200c-ee11-962f-00224859f0ad",
          "unitOfMeasureCode": "CTN",
          "quantity": 1,
          "unitPrice": 17,
          "locationId": "4af3149a-6a0c-ee11-962f-00224859f0ad"
        }
      ]
    }
     
     
    On business central side
     
    procedure ProcessRequest(payload: Text): Text
        var
            SalesHeader: Record "Sales Header";
            SalesLine: Record "Sales Line";
            Base64Convert: Codeunit "Base64 Convert";
            JObject, JOrder, JObjectLine : JsonObject;
            JArray: JsonArray;
            JToken, JTokenValue, JTokenLines : JsonToken;
            JValue: JsonValue;
        begin
            if JObject.ReadFrom(Base64Convert.FromBase64(payload)) then begin
                if JObject.Get('request', JToken) then
                    if JToken.IsObject then begin
                        JOrder := JToken.AsObject();
                        if JOrder.Get('orderDate', JTokenValue) then
                            SalesHeader."Order Date" := JTokenValue.AsValue().AsDate();
    
                        JOrder.Get('salesOrderLines', JTokenLines);
                        JArray := JTokenLines.AsArray();
    
                        foreach JTokenLines in JArray do begin
                            JObjectLine := JTokenLines.AsObject();
                            if JObjectLine.Get('lineType', JTokenValue) then
                                SalesLine.Type := Enum::"Sales Line Type"::Item;
                        end;
                    end;
            end;
        end;
     
    Note that my function parameter has same name as of my json body.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 3,143

#2
Jainam M. Kothari Profile Picture

Jainam M. Kothari 1,694 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 1,067 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans