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

Exporting to JSON Error

(0) ShareShare
ReportReport
Posted on by

I am trying to export data to Json and having some trouble. When I run this I get error that says: The JSON object already contains the key 'Description'. And the same error comes up with whatever line is run where I'm adding. Here is my code, can anyone see why.

codeunit 14135101 "ExportSalesToJson"
{
    procedure ExportSalesInvoiceAsJson(SalesInvoiceHeader: Record "Sales Invoice Header"; var JsonArr: JsonArray)

    var

        SalesLine: Record "Sales Invoice Line";
        SalesInvoiceHeaderJson: JsonObject;
        SalesLineArray: JsonArray;

        SalesIvoiceLineJson: JsonObject;
        SalesInvoiceLine: Record "Sales Invoice Line";


    begin

        SalesInvoiceHeaderJson.Add(SalesInvoiceHeader.FieldCaption("No."), SalesInvoiceHeader."No.");
        SalesInvoiceHeaderJson.Add(SalesInvoiceHeader.FieldCaption("Document Date"), SalesInvoiceHeader."Document Date");
        SalesInvoiceHeaderJson.Add(SalesInvoiceHeader.FieldCaption("Posting Date"), SalesInvoiceHeader."Posting Date");
        SalesInvoiceHeaderJson.Add(SalesInvoiceHeader.FieldCaption("Posting Description"), SalesInvoiceHeader."Posting Description");


        SalesInvoiceLine.Reset();
        SalesInvoiceLine.SetRange("Document No.", SalesInvoiceHeader."No.");
        if SalesInvoiceLine.FindSet() then
            repeat

                SalesIvoiceLineJson.Add(SalesInvoiceLine.FieldCaption("No."), SalesInvoiceLine."No.");
                SalesIvoiceLineJson.Add(SalesInvoiceLine.FieldCaption(Description), SalesInvoiceLine.Description);
                SalesIvoiceLineJson.Add(SalesInvoiceLine.FieldCaption(Type), Format(SalesInvoiceLine.Type));
                SalesIvoiceLineJson.Add(SalesInvoiceLine.FieldCaption("Unit Price"), SalesInvoiceLine."Unit Price");
                SalesIvoiceLineJson.Add(SalesInvoiceLine.FieldCaption(Amount), SalesInvoiceLine.Amount);
                SalesIvoiceLineJson.Add(SalesInvoiceLine.FieldCaption(Quantity), SalesInvoiceLine.Quantity);
                SalesLineArray.Add(SalesIvoiceLineJson);
            until SalesInvoiceLine.Next() = 0;
        SalesInvoiceHeaderJson.Add('Lines', SalesIvoiceLineJson);
        JsonArr.Add(SalesInvoiceHeaderJson);

    end;



    procedure ExportDocs()
    var
        SalesInvHeader: Record "Sales Invoice Header";
        JsonArr: JsonArray;
        Tempblob: Codeunit "Temp Blob";
        IStream: InStream;
        OStream: OutStream;
        ExportFileName: Text;
    begin
        SalesInvHeader.Reset();
        SalesInvHeader.FindSet();
        repeat
            ExportSalesInvoiceAsJson(SalesInvHeader, JsonArr);
        until SalesInvHeader.Next() = 0;

        Tempblob.CreateOutStream(OStream);

        IF JsonArr.WriteTo(OStream) THEN begin
            ExportFileName := 'Sales Invoices.json';
            Tempblob.CreateInStream(IStream);
            DownloadFromStream(IStream, '', '', '', ExportFileName);
        end;

    end;

  • Suggested answer
    Saurav.Dhyani Profile Picture
    14,312 Super User 2025 Season 1 on at
    RE: Exporting to JSON Error

    Hi,

    There is a sample here for Json Export and Import.

    Exporting Data - https://youtu.be/RG0d5vdV_cE 

    Importing Data - https://youtu.be/8qhda8Nak8I

    Source Code - https://github.com/postsaurav/Json-Import-Export

  • Community Member Profile Picture
    on at
    RE: Exporting to JSON Error

    That was it, Thanks a ton!

  • Suggested answer
    Inge M. Bruvik Profile Picture
    999 Moderator on at
    RE: Exporting to JSON Error

    Ok, then i understand.

    I just tried to run it on all salesheaders.

    Maybe if you clear your json variable as the first thing in your repeat loop:

    Clear(SalesIvoiceLineJson);

    Then you start every line with a empty object.

  • Community Member Profile Picture
    on at
    RE: Exporting to JSON Error

    When I initially run it I don't get any errors as well. But in Business Central when I look up Sales Invoices and go into that. I choose one from the list open it and then try to export to Json (I made a button to do that), that is where I get my error and it is at line 28 and in Business Central I get the error The JSON object already contains the key 'No.'. If I comment that part out and run it again it does the same but at line 29 and the error is The JSON object already contains the key 'Description'

  • Suggested answer
    Inge M. Bruvik Profile Picture
    999 Moderator on at
    RE: Exporting to JSON Error

    I tried to run the code, i did not get an error but at the same time i did not get any Json file either.

    Have you run the debugger to check where the code stops?

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 293,280 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,044 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans