Skip to main content

Notifications

Announcements

No record found.

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 151
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."
    }
}
  • Suggested answer
    Ramiz Profile Picture
    Ramiz 471 on at
    Not able to read JSON from Code unit API in Business Central
    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.
  • Suggested answer
    YUN ZHU Profile Picture
    YUN ZHU 76,516 Super User 2024 Season 2 on at
    Not able to read JSON from Code unit API in Business Central

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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Share Your Event with the Community

Jump start your event engagement! 📢

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,717 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans