How to export my JSON File in TEXT File & Save it on my desktop

We are working with NAV_BC14 using C/AL code.


My Jason Code is as follow:
****************************
//
Create Json
IF ISNULL(StringBuilder) THEN
Initialize;

SalesHeader.SETRANGE("Document Type",SalesHeader."Document Type"::Order);
SalesHeader.SETFILTER("No.",'2258634');
IF SalesHeader.FINDFIRST THEN
//JsonTextWriter.WriteStartArray;
BEGIN REPEAT
JsonTextWriter.WriteStartObject;
JsonTextWriter.WritePropertyName('DocumentNo');
JsonTextWriter.WriteValue(SalesHeader."No.");
//Create Branch of Address Start
JsonTextWriter.WritePropertyName('Address');
JsonTextWriter.WriteStartObject;
JsonTextWriter.WritePropertyName('Address1');
JsonTextWriter.WriteValue(SalesHeader."Sell-to Address");
JsonTextWriter.WritePropertyName('City');
JsonTextWriter.WriteValue(SalesHeader."Sell-to Customer Name");
JsonTextWriter.WriteEndObject;
//Create Branch of Address End
//Create Array of Item Details Start
JsonTextWriter.WritePropertyName('ItemDetails');
JsonTextWriter.WriteStartArray;
SalesLine.SETRANGE("Document Type",SalesHeader."Document Type");
SalesLine.SETRANGE("Document No.",SalesHeader."No.");
IF SalesLine.FINDSET THEN
REPEAT
JsonTextWriter.WriteStartObject;
JsonTextWriter.WritePropertyName('ItemNo');
JsonTextWriter.WriteValue(SalesLine."No.");
JsonTextWriter.WritePropertyName('PrName');
JsonTextWriter.WriteValue(SalesLine.Description);
JsonTextWriter.WriteEndObject;
UNTIL SalesLine.NEXT =0;
JsonTextWriter.WriteEndArray;
JsonTextWriter.WriteEndObject;
UNTIL SalesHeader.NEXT =0;
END;
JsonTextWriter.Flush;
JSonString:=GetJSon;
MESSAGE(JSonString);



now how can I export this String as a Text File? can anyone help?




Answers

  • ResolusResolus Member Posts: 25
    edited 2022-09-21
    Create a file, create an outstream, write the string to it, close the file, then open it

    I normally create a function which asks for the string as a parameter, purely to check the full result.
    If you create a second parameter for file location you could use it to really save the Json somewhere dynamically, and reuse the function if needed.
    Variables:
    l_File: File		
    l_OutStream: OutStream		
    l_FileMgt: Codeunit	File Management	
    l_ServerFile: Text		
    l_ClientFile: Text	
    	
    Parameters:
    p_Content: Text		
    
    //// Function SaveToFile(...)
    // Write to file
    l_ServerFile := l_FileMgt.ServerTempFileName('.Json');
    l_File.TEXTMODE(TRUE);
    l_File.CREATE(l_ServerFile);
    l_File.CREATEOUTSTREAM(l_OutStream);
    l_OutStream.WRITE(p_Content);
    l_File.CLOSE();
    
    // Download file to client
    l_ClientFile := l_FileMgt.DownloadTempFile(l_ServerFile);
    l_FileMgt.DeleteServerFile(l_ServerFile);
    HYPERLINK(l_ClientFile);
    
Sign In or Register to comment.