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

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,111 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,111 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,380 Super User 2025 Season 2 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

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,577

#2
YUN ZHU Profile Picture

YUN ZHU 888 Super User 2025 Season 2

#3
Jainam M. Kothari Profile Picture

Jainam M. Kothari 778 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans