Since PowerShell is based on .NET and makes it easy to access .NET functionality, PowerShell can use JSON serializer that comes with .NET. The follow snippet of code shows how to deserialize a JSON string into an XML object and then how to take an XML object and serialize it to JSON:
Add-Type -Assembly System.ServiceModel.Web,System.Runtime.Serialization function Convert-JsonToXml([string]$json) { $bytes = [byte[]][char[]]$json $quotas = [System.Xml.XmlDictionaryReaderQuotas]::Max $jsonReader = [System.Runtime.Serialization.Json.JsonReaderWriterFactory]::CreateJsonReader($bytes,$quotas) try { $xml = new-object System.Xml.XmlDocument $xml.Load($jsonReader) $xml } finally { $jsonReader.Close() } } function Convert-XmlToJson([xml]$xml) { $memStream = new-object System.IO.MemoryStream $jsonWriter = [System.Runtime.Serialization.Json.JsonReaderWriterFactory]::CreateJsonWriter($memStream) try { $xml.Save($jsonWriter) $bytes = $memStream.ToArray() [System.Text.Encoding]::UTF8.GetString($bytes,0,$bytes.Length) } finally { $jsonWriter.Close() } } $str = '{"fname":"John", "lname":"Doe", "age":42, "addr":{"city":"Gotham", "street1":"123 Guano Way", "state":"NY"}}' $xml = Convert-JsonToXml $str $xml.root.fname $xml.root.lname $json = Convert-XmlToJson $xml $json
Note that this does require .NET 3.5. However it serves to show that when you’re looking for some functionality, if you can’t find it in cmdlet or module form first, check the .NET framework next. You would be surprised what kind of cool functionality you can find in there. There’s a bit more to this story. You can use the WCF DataContractJsonSerializer to bypass XML and directly serialize/deserialize .NET types. I can’t take credit for this idea though – that was Jaykul’s.
Hi Keith! I realize that this is a pretty old blog post, but I just came across it and seem to get a weird result. When I try to run the Convert-JSONtoXML function against a variable which has JSON data, all that is returned is the root namespace:
PS C:\Users\m> Convert-JsonToXml($json)
root
—-
root
PS C:\Users\Administrator> $json
{
“images”: [
{
“status”: “SAVING”,
“updated”: “2012-08-15T17:10:39Z”,
“links”: ” “,
“id”: “xxx”,
“name”: “xxx”,
“created”: “2012-08-15T17:10:39Z”,
“minDisk”: 40,
“server”: “xxx”,
“progress”: 25,
“minRam”: 1024,
“metadata”: “xxx”
},
The JSON variable content is massive, so I didn’t put all of it here, but have you seen any output like this before? I’m a little too new to the whole JSON/XML conversation to be able to figure this out, and was hoping to find someone who’d solved this one. Essentially, I’m trying to get this massive list of JSON data into a format where I can play with the output a bit.
Many thanks up front for any time you can spare!
Hi Mitch,
What you’re seeing is just the way PowerShell displays an XML doc by default e.g.:
Try this looking at the xml like so:
Please use tags [ sourcecode language = ], and your code will be easier to copy:
And thank you for your post.
Thanks for the tip. I wasn’t aware of that tag but I will definitely use it in the future.
Also, how to serialize powershell objects into JSON? For example result
Get-EventLog
command?If you have PowerShell v3 or later, use the ConvertTo-Json command.
Get-EventLog Application -EntryType Error | select -First 10 | ConvertTo-Json
Yeah, thats very nice cmdlet, but I had requirement to support also v2 😦 . In the end I decided to use simple string formatting, because the structure of final JSON is known.
What about UTF8-Support? When I Try to Use it, with those german “ü” it doesnt work… Where can i set the Encoding?
I assume you’re referring to the Convert-JsonToXml method? If so, check out this overload for CreateJsonReader – https://msdn.microsoft.com/en-us/library/bb919481(v=vs.110).aspx