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
- We added the following include Filename filter because we only want to scan for *.xml files:
- We’ll need a Pascal Script to convert the xml file to a Json file so we added Pascal Script as Destination:
- 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
- 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.
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.
- 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.
#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