web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Business Central forum

Creating JSON data in AL JsonObjectClass

(0) ShareShare
ReportReport
Posted on by

Hi

Sample code needed to create JSON data using JsonObjectClass in AL using Visual studio code

 

Thanks

Seema

Categories:
  • Community Member Profile Picture
    on at

    Hi Seema,

    1) have a look on this article (you can use it as a white paper for how to work with JSON with AL)

       https://www.kauffmann.nl/2017/06/24/al-support-for-rest-web-services/

    2) There is a nice extension for Visual Studio Code by Tobias Fenster that auto-generate AL code based on a JSON file: 

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

       Note: The Tobias's extension is based on the first article above.

    .

    Please verify my answer if you find my answer helpful. Doing so you'll show other community members that there was found a solution and you credit my help.

    ,

    Best regards

  • Community Member Profile Picture
    on at

    Hi Nissay,

    Thanks for link , the Article is good for reading JSON data with AL . I want code creating JSON data in AL using JSONOBJECT, ARRAY etc  

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

    Thanks

    Seema ANM

  • Suggested answer
    Community Member Profile Picture
    on at

    Hi again Seema,

    Sorry I misunderstood your need.

    Well this is an example on how to create a JSON with AL for a Sales Order (Sales Header and Sales Line).

    1) This is the resulted JSON file/the JSON to create:

    json.png

    2) Here is the code in AL:

        procedure CreateJsonProcedure()
        var
            VJsonObjectHeader: JsonObject;
            VJsonObjectLines: JsonObject;
            VJsonArray: JsonArray;
            VJsonArrayLines: JsonArray;
            VSalesHeader: Record "Sales Header";
            VSalesLines: Record "Sales Line";
        begin
    
            // Header
            VSalesHeader.Reset();
            VSalesHeader.SetFilter("Document Type", '%1', VSalesHeader."Document Type"::Order);
            VSalesHeader.FindLast();
    
            VJsonObjectHeader.Add('Sales Order No.', VSalesHeader."No.");
            VJsonObjectHeader.Add('Bill-to Name', VSalesHeader."Bill-to Name");
            VJsonObjectHeader.Add('Order Date', VSalesHeader."Order Date");
    
            VJsonArray.Add(VJsonObjectHeader);
    
            //Lines
            VSalesLines.Reset();
            VSalesLines.SetFilter("Document Type", '%1', VSalesLines."Document Type"::Order);
            VSalesLines.SetFilter("Document No.", VSalesHeader."No.");
            VSalesLines.FindSet();
    
            // JsonObject Init
            VJsonObjectLines.Add('Line No.', '');
            VJsonObjectLines.Add('Item No.', '');
            VJsonObjectLines.Add('Location Code', '');
            VJsonObjectLines.Add('Quantity', '');
    
            repeat
    
                VJsonObjectLines.Replace('Line No.', VSalesLines."Line No.");
                VJsonObjectLines.Replace('Item No.', VSalesLines."No."); 
                VJsonObjectLines.Replace('Location Code', VSalesLines."Location Code");
                VJsonObjectLines.Replace('Quantity', VSalesLines.Quantity);
    
                VJsonArrayLines.Add(VJsonObjectLines);
    
            until VSalesLines.Next() = 0;
    
            VJsonArray.Add(VJsonArrayLines);
    
        
        end;


    You can use the Message function to view the structure of your JSON :

            VJsonText: Text;  // in Var section
    VJsonArray.WriteTo(VJsonText); Message('My JSON : %1', VJsonText);

    .

    Please verify my answer if you find my answer helpful. Doing so you'll show other community members that there was found a solution and you credit my help.

    ,

    Best regards

  • chou chihming Profile Picture
    on at
    VJsonObjectLines.Replace('Line No.', VSalesLines."Line No.");

    [

    {
    "Line No.": 20000,
    "Item No.": "1964-W",
    "Location Code": "GREEN",
    "Quantity": 2.0
    },
    {
    "Line No.": 20000,
    "Item No.": "1964-W",
    "Location Code": "GREEN",
    "Quantity": 2.0
    }
    ]
  • Fommo Profile Picture
    5 on at

    As the Json data types are references you need to use the Clone function when adding to the array:

     VJsonArrayLines.Add(VJsonObjectLines.Clone());

    This will create a deep copy of the JsonObject and add it to the array. This way you can use Replace on the VJsonObjectLines without replacing the data that is already added to the JsonArray.

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard > Business Central

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans