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...
Answered

Consume Json in Business Central

(0) ShareShare
ReportReport
Posted on by

Hello every,

Trying to consume a json file like this:

{
    "salesinvoiceno""PS-INV103196",
    "value": [
        {
            "profileid""1896-S",
            "desc""ATHENS Desk"
        },
        {
            "profileid""1936-S",
            "desc""BERLIN Guest Chair, yellow"
        },
        {
            "profileid""1972-S",
            "desc""MUNICH Swivel Chair, yellow"
        },
        {
            "profileid""1996-S",
            "desc""ATLANTA Whiteboard, base"
        }
    
}
Have used some approach to get the object of "value "array but unable to get then object in repeat. Kindly guide to achieve the objects in "value" array to loop through. .

JToken.ReadFrom(JsonText);
JToken.SelectToken('value', JsonToken1);
JArray := JsonToken1.AsArray();

for i := 0 to JArray.Count do begin
JArray.Get(i, JToken1);
Jobject := JToken1.AsObject();
codeunit 50107 SalesInvoicejSON1
{
    TableNo = 112;
    Permissions = tabledata 112 = rim;

procedure InsertPostedOpportunity(SinvNo: Code[20])
    var

        JToken: JsonToken;
        JArray: JsonArray;
        JValue: JsonValue;
        Jobject: JsonObject;
        JsonObject1: JsonObject;
        JsonToken1: JsonToken;

        JToken1: JsonToken;
        i: Integer;
    begin
        Clearall();
        Clear(JsonText);

        GenerateJsonPosted(SInvNo);
        Message(JsonText);

        JToken.ReadFrom(JsonText);
        JToken.SelectToken('value', JsonToken1);
        JArray := JsonToken1.AsArray();

        for i := 0 to JArray.Count do begin
            JArray.Get(i, JToken1);
            Jobject := JToken1.AsObject();
        end;

    end;

    local procedure ValidateJsonToken(JsonObject: JsonObject; TokenKey: text) JsonToken: JsonToken
    begin
        if not JsonObject.Get(TokenKey, JsonToken) then
            Error('Could not find token with key: %1', TokenKey);
    end;
    [TryFunction]
    procedure InsertOpportunity(SInvNo: Code[20])
    var
        myInt: Integer;
    begin

        InsertPostedOpportunity(SInvNo);
    end;




    local procedure GenerateJsonPosted(SINNo: Code[20])
    var

        JObject: JsonObject;
        Jarray: JsonArray;
        JBranch: JsonObject;
        JLineObj: JsonObject;

        SILine: Record "Sales Invoice Line";
        SIHeader: Record "Sales Invoice Header";
        cUSTOMER: Record Customer;
    begin
        Clear(Jobject);
        Clear(JArray);
        Clear(JBranch);
        Clear(JLineObj);
        Clear(JsonText);
        SIHeader.Reset();
        SIHeader.SetRange("No.", SINNo);
        if SIHeader.FindFirst() then begin
            SIHeader.CalcFields("Amount Including VAT");
            Jobject.Add('salesinvoiceno', SIHeader."No.");
            SILine.Reset();
            SILine.SetRange("Document No.", SIHeader."No.");
            SILine.SetFilter("No.", '<>%1', '');
            SILine.FindSet();
            //Line Details --Start
            JLineObj.Add('profileid', '');
            JLineObj.Add('desc', '');
            repeat
                JLineObj.Replace('profileid', SILine."No.");
                JLineObj.Replace('desc', SILine.Description);

                JArray.Add(JLineObj.Clone());
            //Line Details -------End
            until SILine.Next() = 0;
            //Creating Json ---Start
            Jobject.Add('value', JArray);
            Jobject.WriteTo(JsonText);
            //Creating Json --END

        end;
    end;



    

    var
        JsonText: Text;

}

// Welcome to your new AL extension.
// Remember that object names and IDs should be unique across all extensions.
// AL snippets start with t*, like tpageext - give them a try and happy coding!

pageextension 50100 CustomerListExt extends "Posted Sales Invoices"
{

    actions
    {

        addafter(Navigate)
        {
            action(ConsumeJson)
            {
                Caption = 'Consume Json';
                Promoted = true;
                PromotedOnly = true;
                PromotedCategory = Process;
                ApplicationArea = all;
                PromotedIsBig = true;
                ToolTip = 'Consume Json';
                trigger OnAction()
                var
                    Cu: Codeunit 50107;
                begin
                    Cu.InsertPostedOpportunity(Rec."No.");
                end;

            }
        }
    }
}
I have the same question (0)
  • Verified answer
    Marco Mels Profile Picture
    on at

    Hello,

    We currently do not have dedicated Dev support via the Dynamics 365 Business Central forums, but I wanted to provide you some additional resources to assist.  If you need assistance with debugging or coding I would recommend discussing this on one of our communities.

    www.yammer.com/dynamicsnavdev

    dynamicsuser.net/.../developers

    I will open this up to the community in case they have something to add.

    Thanks.

  • Suggested answer
    JAngle Profile Picture
    133 on at

    Full context not known so my answer might seem unhelpful at first...

    Is there a reason for overlooking logic apps and power automate for this process? Not only can it handle looping through a JSON array it will also enable you to trigger specific actions like invoice posting. In this code it seems that you are passing the data straight to the posted sales invoice tables. This might be known by you and is on purpose but that won't create the g/l and customer ledger entries to show it as a legitimate sale. So you won't record any revenue for it.

    Perhaps me taking things literally as they are written down but Opportunities sit in the CRM part of the system and can be tracked from quote all the way through to invoice.

  • Community Member Profile Picture
    on at

      for i := 0 to JArray.Count - 1 do begin

               JArray.Get(i, JToken1);

               Jobject := JToken1.AsObject();

               ProfileId := ValidateJsonToken(Jobject, 'profileid').AsValue().AsText();

               Message('Profile ID %1 AND Count %2', profileid, Format(i));

           end;

    Adding this code done the deal. thanks Marco

  • Community Member Profile Picture
    on at

    Yes , I receive same format from CRM and have to insert in Business Central.

    In Line where JArray.Count is written added JArray.Count-1 and it executes as expected

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 2,362

#2
YUN ZHU Profile Picture

YUN ZHU 867 Super User 2025 Season 2

#3
Sumit Singh Profile Picture

Sumit Singh 607

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans