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 :
Microsoft Dynamics NAV (Archived)

how to convert text to JsonArray in D365 BE

(0) ShareShare
ReportReport
Posted on by

hi experts,

i am creating an app using AL language.
in this code, i need to use a json code.
however i couldn't convert json result to jsonArray.


when running this code,

here is the code.
// Process JSON response
        Clear(JsonArray);
        JsonArray := ReadJson(JsonText);

local procedure ReadJson(data : Text) result : JsonArray;
    begin
        Message(data); //todo : remove
        if not result.ReadFrom(data) then
            Error('Invalid response, expected an JSON array as root object');  
    end;

here is the response data, JsonText
{
  "results": {
    "common": {
      "errorMessage": "success",
    },
    "target": [
      {
        "liNm": "",
        "rnMgtSn": "116502102001",
        "mtYn": "0",
        "bdMgtSn": "1165010800113280001000001"
      }
    ]
  }
}

thank you in advance.

jk

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Andrey Baludin Profile Picture
    3,941 on at

    Hi!

    I use this article as a white papers how to work with JSON from AL :) :

    www.kauffmann.nl/.../al-support-for-rest-web-services

    Also, there is one nice extension for vs code by Tobias Fenster, which generates AL objects by API url (based on previous article):

    www.axians-infoma.de/.../generating-al-objects-from-json

  • jadenkim Profile Picture
    on at

    thank you for your reply, Andery.

    this article is helpful for me.

  • jadenkim Profile Picture
    on at

    Hi andrey,

    i read the article and i tried to handle the json.

    finally i didnt' do it.

    because json file that i have is more complex than the sample.

    can you advice me how to handle this json?

    {

     "results": {

       "common": {

         "errorCode": "0",

         "currentPage": "1"

       },

       "data": [

         {

           "admCd": "1165010800",

           "udrtYn": "0",

           "lnbrMnnm": "1328"

         },

         {

           "admCd": "1168011800",

           "udrtYn": "0",

           "lnbrMnnm": "199"

         },

         {

    "admCd": "1168011800",

           "udrtYn": "0",

           "lnbrMnnm": "194"

         },

         {

           "admCd": "1168011800",

           "udrtYn": "0",

           "lnbrMnnm": "199"

         }

       ]

     }

    }

  • Suggested answer
    Andrey Baludin Profile Picture
    3,941 on at

    I understood your problem, it,s because Json Array should starts from square brackets [, and your response starts from { and array data[] placed inside.

    Honestly, i don't know how to get array from this response properly, so i used a cheat (if anybody knows - i'll be very appreciate):

           if not Client.Post("API URL", Content, ResponseMessage)

           then

               Error('The call to the web service failed.');

           ResponseMessage.Content.ReadAs(JsonText);

    //Here is we get a clear array by copying ony part in square brackets

           JsonText := CopyStr(JsonText, StrPos(JsonText, '['));

           JsonText := CopyStr(JsonText, 1, StrLen(JsonText) - 1);

    // Process JSON response

           if JsonArray.ReadFrom(JsonText) then begin

               for i := 0 to JsonArray.Count - 1 do

               begin

                   JsonArray.Get(i, JsonToken);

                   JsonObject := JsonToken.AsObject;

                   //and here some function which takes data from JsonObject like in article

               end;

           end;

    And also place a breakpoints and use a debugger - this way you'll get a step on which error occurs.

  • jadenkim Profile Picture
    on at

    i solve this.

    thank you andrey.

  • Community Member Profile Picture
    on at

    Hi JK

    It Will be Great Help If You Explain how you Solve. If Using JSONARRAY  give your code

    Thanks

    Seema

  • Andrey Baludin Profile Picture
    3,941 on at

    Hi Seema, try this:

    ResponseMessage.Content.ReadAs(JsonText);
    JsonToken.ReadFrom(JsonText);
    JsonObject := JsonToken.AsObject;
    JsonObject.SelectToken('data', JsonToken);
    JsonArray := JsonToken.AsArray;
    for i := 0 to JsonArray.Count - 1 do
    begin
      JsonArray.Get(i, JsonToken);
      HandleArray(JsonToken);
    end;  
    
    procedure HandleArray(JsonToken : JsonToken);
        var
            JsonObject : JsonObject;
            Results : Record "XXX"; // <- Your table
        begin
            JsonObject := JsonToken.AsObject;
    
            Results.init;
            Results.admCd := SelectJsonToken(JsonObject, '$.admCd')
            Results.udrtYn := SelectJsonToken(JsonObject, '$.udrtYn')
            Results.lnbrMnnm := SelectJsonToken(JsonObject, '$.lnbrMnnm')        
            Results.Insert;
        end; 
    
    
    local procedure SelectJsonToken(JsonObject: JsonObject; Path: text): text;
        var
            JsonToken: JsonToken;
        begin
            if not JsonObject.SelectToken(Path, JsonToken) then
                exit('');
            if JsonToken.AsValue.IsNull then
                exit('');
            exit(jsontoken.asvalue.astext);
        end; 


      

  • Community Member Profile Picture
    on at

    How do I do that?

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 > 🔒一 Microsoft Dynamics NAV (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans