XLS/CSV

  • 31 May

    How to convert from XML to JSON format

    Q: We receive some files in XML format. Could you please confirm, if Limagito tool can convert XML files to JSON format? If yes, Do we have any limitations while converting the file type? Please let us know if you need any additional information.

    A: We added an option in version v2023.5.31.0 to achieve this.

    • Our Source will be a Windows folder

    limagito file mover Windows folder as source

    • We added the following include Filename filter because we only want to scan for *.xml files:

    limagito file mover filename filter

    • We’ll need a Pascal Script to convert the xml file to a Json file so we added Pascal Script as Destination:

    limagito file mover destination setup

    • We noticed that the xml we received from the customer was not a valid xml so we added some extra code (line 18) in the script to adjust this

    limagito filemover xml to json

    • Pascal Script we used (don’t forget to adjust the ctOutputPath const):
    Var
     tmpFileInput, tmpFileOutput, tmpResult: String;
     tmpXmlText: String;
     tmpList: TStringList;
    Const
      ctOutputPath = 'C:\Test\Out_JSon\'; // Must end with a \ 
    Begin
      // Init Var
      tmpFileInput := psFilePath + psFileName;
      tmpFileOutput := ctOutputPath + ChangeFileExt(psFileName, '.Json');
      psExitCode:= 0;
      // Read non valid Xml file and adjust before creating Json File
      tmpList := TStringList.Create;
      Try
        Try
          tmpList.LoadFromFile(tmpFileInput);
          tmpXmlText := tmpList.Text;
          tmpXmlText := psStringReplace(tmpXmlText, '-<', '<'); // Adjust Non Valid Xml
          // psLogWrite(1, '', 'StringReplace Xml Result: ' + tmpXmlText); // Debug
          tmpResult := psXmlTextToJSonFile(tmpXmlText, tmpFileOutput, True, True, True, True);
          If tmpResult = '' Then
          Begin
            psLogWrite(1, '', 'Saved Json Data to ' + tmpFileOutput);      
            psExitCode := 1;
          End  
          Else
            psLogWrite(1, '', tmpResult);
        Except
          psLogWrite(1, '', 'Load Xml from ' + tmpFileInput + ' Exception');
        End;
      Finally
        tmpList.Free;
      End;
    End.

    limagito file mover convert from xml to json

    In the example above we used The following function:

    Function psXmlTextToJSONFile(Const aXmlText, aJsonFile: String; Const aXmlParse, aXmlPack, aJsonIndent, aJsonEscape: Boolean): String;

    The psXML functions have a aXmlPack option that determines the format returned, Pack = False returns all tags as values with multiple nested levels of objects, Pack = True is simpler and easier to process but does lose some tag names.

    aXmlPack = False : all XML content is converted into Json values, each level with two or three names: #attributes is an optional object from opening tag, #Name
    is the string from <Tag>, and #children which is an array of other Json objects and arrays nested within the tag.

    aXmlPack = True : tags are converted to Names and their content to Values, or nested objects. For a single record, a single object is returned with the tag names
    lost, for multiple records (ie tags of the same name) an array is created with each record being an object.

     

    • RunTime Log Result:

    limagito file mover xml to json runtime log result

    • Json File Result:

     

    limagito filemover xml to json

    • Another available Pascal Script function that can be used only when source xml is valid (direct file to file conversion):

    Function psXmlFileToJSONFile(Const aXmlFile, aJsonFile: String; Const aXmlPack, aJsonIndent, aJsonEscape: Boolean): String;

    • Pascal Script will result in:
    Var
     tmpFileInput, tmpFileOutput, tmpResult: String;
    Const
      ctOutputPath = 'C:\Test\Out_JSon\'; // Must end with a \ 
    Begin
      // Init Var
      tmpFileInput := psFilePath + psFileName;
      tmpFileOutput := ctOutputPath + ChangeFileExt(psFileName, '.Json');
      psExitCode:= 0;
      // Xml File To JSon File
      tmpResult := psXmlFileToJSonFile(tmpFileInput, tmpFileOutput, True, True, True);
      If tmpResult = '' Then
      Begin
        psLogWrite(1, '', 'Saved Json Data to ' + tmpFileOutput);  
        psExitCode := 1;
      End  
      Else
        psLogWrite(1, '', tmpResult);
    End.
    

    limagito file mover convert from xml to json

    #filetransfer #mft #filemanagement #xml #json

    If you need any info  about this ‘Convert from XML to JSON format’ request, please let us know.

    Best regards,

    Limagito Team

  • 10 Feb

    Move and change content of a txt-file

    Q: Hello Limagito, Now I need your help again…

    I got a TXT-file delivered from my customer with approximately 250 rows.

    In the txt file I like “search and replace” a text string :

    ;19;18;;

    Should be replaced with

    ;36;35;;

    And the string

    ;21;21;;

    Should be replaced with

    ;37;37;;

    I also tried to set the powershell exe file in the field for Application name (C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe)

    Is there any other way?

    A: Yes there is, we can do this easily with our Pascal Script so no need for external scripts if you want.

    In our example we’ll use a Windows folder as Source:

    Limagito File Mover WIN as Source

    We only want to handle .txt file so we added the following Filename Include Filter:

    Limagito File Mover Include Filename Filter

    As Destination we added a Pascal Script. This script will does the work you requested.

    Limagito File Mover Pascal Script as Destination

    • Adjust ctOutputPath > must end with a \
    • Encoding of destination text file can be adjusted

     

    Var
      tmpFileIn, tmpFileOut: String;
      tmpList: TStringList;
    Const
      ctOutputPath = 'C:\Test\Out_Txt\';
    Begin
      // Init var
      psExitCode:= 0;
      tmpFileIn := psFilePath + psFileName;
      tmpFileOut := ctOutputPath + psFileName; 
      // ... add your code here
      tmpList := TStringList.Create;
      Try
        Try
          tmpList.LoadFromFile(tmpFileIn);
          tmpList.Text := psStringReplace(tmpList.Text, ';19;18;;', ';36;35;;');
          tmpList.Text := psStringReplace(tmpList.Text, ';21;21;;', ';37;37;;');
          // https://docs.microsoft.com/nl-be/windows/win32/intl/code-page-identifiers
          If psSaveTxt2File(tmpList.Text, ctOutputPath + psFileName, 'ISO-8859-1', False) Then
          Begin
            psExitCode := 1;
            psLogWrite(1, '', 'SaveToFile ' + tmpFileOut + ' Successful');    
          End
          Else
            psLogWrite(1, '', 'SaveToFile ' + tmpFileOut + ' Error');
        Except
          psLogWrite(1, '', 'LoadFromFile ' + tmpFileIn + ' Exception');
        End;
      Finally
        tmpList.Free;
      End;  
    End.

    Limagito File Mover Pascal Script as Destination

    Feedback Customer:

    As always you save the day! The Pascal script did my work easily. Some day I have to learn more about pascal!

    I cheat some in powershell and some visual basic, but this stuff is powerful!

    By the way, it only worked in the newer version of Limagito (I still have some workflows in the old version)

    Thank you for help, quick and accurate, as always!

    #FileTransfer #Encoding

    If you need any info about this ‘change content of a txt-file’ request, please let us know.

    Best regards,

    Limagito Team

  • 31 Mar

    Csv to Xls conversion problems

    Q: Csv to Xls conversion problems. I have a small problem with the conversion of .csv to .xls files. My original file is separated by ; saved as .txt.

    • I also tried with a comma separated .csv and I get the same result.
    • I also tried to import full csv as text
    • I also tried adding a header
    • I still get the same result

    I have attached some screenshots, I have also attached the txt file to convert. My file contains data in French with accents, can this have an impact?

    A: It looks like a problem with the encoding of the txt file. We converted it to UTF8 first using a Pascal Script and afterwards the conversion worked just fine.

    1. Open the Pascal Script option:

    Limagito File Mover Pascal Script option

    2. Enable and add the following ‘On Destination’ Pascal Script. This will first change the Encoding of the Source txt file to UTF8. The conversion to XLS was ok afterwards.

    Var
      tmpFile: String;
    Begin
      psExitCode:= 0;
      // ... add your code here
      tmpFile := psFilePath + psFileName;
      If psChangeTxtEncoding(tmpFile, tmpFile, 0) Then
        psExitCode := 1;
    End.

    Limagito File Mover conversion problems

    More information about the “psChangeTxtEncoding function”:

    Use psChangeTxtEncoding to convert the encoding

    > Encoding possibilities:
    0: UTF8
    1: UTF7
    2: Unicode
    3: Default
    4: BigEndianUnicode
    5: ASCII
    6: ANSI
    7: ASCII TRIM
    8: ANSI TRIM

    i.e. psChangeTxtEncoding(psFilePath + psFileName, psFilePath + psFileName, 0);

    This script will change the encoding of the original source file before it is converted to .xls using XLS as Destination.

    Some extra screenshots of the XLS as Destination setup:

    Limagito File Mover conversion problems

    Limagito File Mover conversion problems

    #Filetransfer #csv #xls

    If you need any info about this ‘conversion problems’ issue, please let us know.

    Best regards,

    Limagito Team

1 2 3 4
SEARCH