StdOut - PS Pipeline and Json API query

Started by spl, March 14, 2026, 06:08:13 AM

Previous topic - Next topic

spl

The script below works with fake json return data. The script has 2 sets of the args variable to secure and display 'customer' data in json format. Both return the same data and are pretty much word for word the distinction being when the pipeline | is on the same line as the request, PS adds both a "valid": element as well as a count. Probably no one reading this will really care or even try it, but it is an interesting distinction.

[EDIT]
P.S. If there are concerns about this, I can post pure WB code to return the json data... that is not the point. The WB code will return the same json as the first args set. It is the 2nd set adding a 'pseudo' header element that is.

;Subtle difference with using PS Pipeline with Json return data
;Displays Json return as text file
;Stan Littlefield 3/14/2026
;========================================================================================================================
Gosub udfs
file = Dirscript():"Customers.json"
If fileexist(file) Then filedelete(file)
;this will return json without a "valid": data element
args = $"$url = 'https:://fake-json-api.mock.beeceptor.com/customers'    
$json=Invoke-RestMethod -Uri $url  -Method Get 
$json | ConvertTo-Json
$"
             
;comment above args and uncomment below to obtain "valid": element
;can be replaced with "Customers:" to make json more understandable
;args = $"$url = 'https:://fake-json-api.mock.beeceptor.com/customers'    
;$json=Invoke-RestMethod -Uri $url  -Method Get | ConvertTo-Json
;$json
;$"


cmd="Powershell"
msg='Fake Json Customer Data'
BoxOpen("Running...",cmd:" ":args:@LF:"PLEASE WAIT...MAY TAKE SOME TIME")
TimeDelay(2)
vals = Get_stdout()
FilePut(file,vals) 
If FileExist(file) Then Run("notepad.exe",file)
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]

SMF spam blocked by CleanTalk