Yaml - revisted

Started by spl, October 20, 2024, 06:26:03 AM

Previous topic - Next topic

spl

Earlier this year, this thread ended with "Nope"
https://forum.winbatch.com/index.php?topic=3060.msg17681#msg17681

Below is a WB script using my StdOut function to convert csv into yaml. The 'glue' in all this is moving data into an array. WB can create arrays, persist Json or maps into arrays, so not a big deal to convert to yaml. Yaml is a recognized superset of json with more readability. I have attached a sample csv for testing the script from a C:\temp folder, if persisted there. Of course, there is probably no need for yaml by users on this site, but servers like AWS and companies like IBM, Red Hat like it as do other for as earlier mentioned 'readability'. Oh, and converting json=>yaml is a also a 2 line argument in StdOut which I don't mind posting if any interest here   NOTE: change attached file from .txt to .csv
;Convert CSV file into YAML
;save as ..\CSV2YAML.wbt
;Stan Littlefield 10/20/2024
;==========================================================
Gosub udfs
csv = "C:\temp\food.csv"
If ! FileExist(csv) Then Terminate(2,"Input CSV Missing",csv)

;Arguments will first check for PS yaml cmdlet
;and install if missing
;Then convert csv file to array [which WB could do natively]
;and finally convert array to yaml file type
;adds --- as first line required for yaml

args = $"
$module = 'powershell-yaml'
If (-not(Get-InstalledModule $module -ErrorAction silentlycontinue)) {
  Install-Module -Name $module -Scope CurrentUser -Force
}
Import-Module powershell-yaml
$arr = Import-Csv -Path |file|
$yamlContent = ConvertTo-Yaml -Data $arr
$top = '---'
$yamlContent = $top + $yamlContent
$yamlContent
$"
args = StrReplace(args,"|file|",csv)
cmd="Powershell"
msg='CSV => YAML'

BoxOpen("Running...",cmd:" ":args:@LF:"PLEASE WAIT...MAY TAKE SOME TIME")
TimeDelay(2)
vals = Get_stdout()
vals = StrReplace(vals,'---','---':@CRLF) ;adjust yaml header
Message(msg,vals) ;will probably be messy so consider file out                       
;optional - create output file with links, for further scraping
;FilePut("c:\temp\csv2yaml.yml",vals)
Exit
;==========================================================
:udfs
#DefineSubroutine Get_stdout()
ObjectClrOption("useany","System")
objInfo = ObjectClrNew("System.Diagnostics.ProcessStartInfo")
Output=""
timeOut = ObjectType("I2",5000)
objInfo.FileName = cmd
objInfo.RedirectStandardError = ObjectType("BOOL",@TRUE)
objInfo.RedirectStandardOutput = ObjectType("BOOL",@TRUE)
objInfo.UseShellExecute = ObjectType("BOOL",@FALSE)
objInfo.CreateNoWindow = ObjectType("BOOL",@TRUE)
objInfo.Arguments = args
oProcess = ObjectClrNew("System.Diagnostics.Process")
oProcess.StartInfo = objInfo
BoxShut()
oProcess.Start()
oProcess.WaitForExit(timeout)
STDOUT = oProcess.StandardOutput.ReadToEnd()
STDERR = oProcess.StandardError.ReadToEnd()
Output = Output:STDOUT:@CRLF
If STDERR<>""
   Output = Output:"STDERR:":STDERR:@CRLF
Endif
oProcess = 0
objInfo = 0

Return (Output)
#EndSubroutine
Return
;==========================================================
Stan - formerly stanl [ex-Pundit]

spl

Follow-up from previous post. Attached zip has yaml nested file and script to convert and display as json, which should be valid to load and parse with WB extender.
Stan - formerly stanl [ex-Pundit]

spl

I see no one has tested any of this. I tried parsing the Json created from yaml file with the WB Extender. Indicated valid Json, but  little tricky to parse values. Still worked, so great Extender!
Stan - formerly stanl [ex-Pundit]