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 :
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;

I have the same question (0)
  • Suggested answer
    Inge M. Bruvik Profile Picture
    1,161 Moderator on at

    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?

  • Community Member Profile Picture
    on at

    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
    1,161 Moderator on at

    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

    That was it, Thanks a ton!

  • Suggested answer
    Saurav.Dhyani Profile Picture
    14,414 User Group Leader on at

    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

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 1,965 Super User 2026 Season 1

#2
YUN ZHU Profile Picture

YUN ZHU 1,125 Super User 2026 Season 1

#3
Dhiren Nagar Profile Picture

Dhiren Nagar 961 Super User 2026 Season 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans