WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Cmdlets

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Convert JSON to XML

 Keywords: Convert JSON XML  get-content out-string ConvertFrom-Json export-clixml System.Management.Automation System.Management.Automation.PowerShell BeginInvoke EndInvoke 

;/////////////////////////////////////////////////////////////////////////////////////////////////////////
;Winbatch 2013A - Convert Json to XML (basic)
;                 
; !!! REQUIRES PowerShell 3.0 or newer because of the use of ConvertFrom-Json !!!
;
;Stan Littlefield, May 8, 2013
;/////////////////////////////////////////////////////////////////////////////////////////////////////////
If Version( )< '2013A'
   Pause('Notice', 'Need 2013A or Newer Version of WinBatch')
   Exit
EndIf

cFile=Dirscript():"cars.json"
If ! FileExist(cFile) Then Terminate(@TRUE,"Cannot Continue",cFile:" not found")
cFile1= Dirscript():"cars.xml"
If FileExist(cFile1) Then FileDelete(cFile1)

;generic Powershell Code (should work with both 2.0 and 3.0)
cScript = '$file = "|cFile|"':@CRLF 
cScript = cScript:'$xml = Get-content $file | out-string | ConvertFrom-Json':@CRLF
cScript = cScript:'$xml | export-clixml "|cFile1|"':@CRLF

cScript=StrReplace(cScript,"|cFile|",cFile)
cScript=StrReplace(cScript,"|cFile1|",cFile1)

BoxOpen("Please Wait","Processing... ")

ObjectClrOption("use", "System.Management.Automation,version=1.0.0.0,publicKeyToken=31bf3856ad364e35,culture=neutral")
objAutoPs = ObjectClrNew("System.Management.Automation.PowerShell") 
oPshell = objAutoPs.Create()
oScope = ObjectType("BOOL",@TRUE)
oPshell.AddScript(cScript,oScope)
objAsync = oPshell.BeginInvoke() 
oPShell.EndInvoke(objAsync)      

oPshell.Dispose()
oPshell=0

BoxShut()
If FileExist(cFile1)
   runwait("Notepad",cFile1)
Else
   Display(2,"File Not Created",cFile1)
Endif

Exit

Article ID:   W17829
Filename:   Convert JSON to XML.txt
File Created: 2013:05:09:08:35:10
Last Updated: 2013:05:09:08:35:10