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;
1: Add-Type -Assembly System.ServiceModel.Web,System.Runtime.Serialization
2:
3: function Convert-JsonToXml([string]$json)
4: {
5: $bytes = [byte[]][char[]]$json
6: $quotas = [System.Xml.XmlDictionaryReaderQuotas]::Max
7: $jsonReader = [System.Runtime.Serialization.Json.JsonReaderWriterFactory]::CreateJsonReader($bytes,$quotas)
8: try
9: {
10: $xml = new-object System.Xml.XmlDocument
11:
12: $xml.Load($jsonReader)
13: $xml
14: }
15: finally
16: {
17: $jsonReader.Close()
18: }
19: }
20:
21: function Convert-XmlToJson([xml]$xml)
22: {
23: $memStream = new-object System.IO.MemoryStream
24: $jsonWriter = [System.Runtime.Serialization.Json.JsonReaderWriterFactory]::CreateJsonWriter($memStream)
25: try
26: {
27: $xml.Save($jsonWriter)
28: $bytes = $memStream.ToArray()
29: [System.Text.Encoding]::UTF8.GetString($bytes,0,$bytes.Length)
30: }
31: finally
32: {
33: $jsonWriter.Close()
34: }
35: }
36:
37: $str = '{"fname":"John", "lname":"Doe", "age":42, "addr":{"city":"Gotham", "street1":"123 Guano Way", "state":"NY"}}'
38:
39: $xml = Convert-JsonToXml $str
40: $xml.root.fname
41: $xml.root.lname
42:
43: $json = Convert-XmlToJson $xml
44: $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:
8> $xml | Format-Xml <a> <b> <c/> </b> </a> </pre