PowerShell is great when it comes to working with JSON. Being a scripting language, you can pretty
much de-serialize your json without declaring types for them, do your work on de-serialized objects, and then serialize it back to for storing or transport.
The ConvertFrom-Json and ConvertTo-Json are
powerful functions. However, there are a
few nuggets that you need to be aware of. I got caught out by one of the so
thought to blog about it.
When working with
ConvertTo-Json, be mindful of the -Depth parameter. The parameter specifies how
deep it should go in our object while converting to json string. The default
value is 2. What it means is that if you have a complex json object that goes down more than two levels of depth and it you haven't specified
the -Depth parameter, your nested objects would be treated as
Hashtable.
As an example, let's
assign a json string to a variable
$programmersJson = '[{
"Name" : "Hamid",
"Gender" : "Male",
"Expertise": [
{
"Skill": "PowerShell",
"Level": "5"
},
{
"Skill": "C#",
"Level": "8"
}
]},
{
"Name" : "Adnan",
"Gender" : "Male" ,
"Expertise": [
{
"Skill": "PowerShell",
"Level": "7"
},
{
"Skill": "C#",
"Level": "6"
}
]}]'
The json string contains an object that is three level deep. . The top level object is a
collection, each item in the collection is an object with a "Name" and
"Gender" property as well as a collection of objects each has a "Skill" and "Level" property
Now, lets call ConvertFrom-Json
$programmers = ConvertFrom-Json -InputObject $programmersJson
Write-Output $programmers
The result is an array of objects, as expected
Name Gender Expertise
---- ------ ---------
Hamid Male {@{Skill=PowerShell; Level=5}, @{Skill=C#; Level=8}}
Adnan Male {@{Skill=PowerShell; Level=7}, @{Skill=C#; Level=6}}
Now, lets try to convert it back to json. So, when you call
ConvertTo-Json -InputObject $programmers
You would expect the Json string to be same as $programmersJson. Wrong!! The string you get back is
[
{
"Name": "Hamid",
"Gender": "Male",
"Expertise": [
"@{Skill=PowerShell; Level=5}",
"@{Skill=C#; Level=8}"
]
},
{
"Name": "Adnan",
"Gender": "Male",
"Expertise": [
"@{Skill=PowerShell; Level=7}",
"@{Skill=C#; Level=6}"
]
}
]
Notice, the @ sign for each of the item in the Expertise collection. It means that the function has treated each item as a Hashtable rather than an object.
Now execute the following
ConvertTo-Json -InputObject $programmers -Depth 3
The depth parameter will make it treat each item of Expertise collection as an object as well and the resulting Json would be as you would expect.
[
{
"Name": "Hamid",
"Gender": "Male",
"Expertise": [
{
"Skill": "PowerShell",
"Level": "5"
},
{
"Skill": "C#",
"Level": "8"
}
]
},
{
"Name": "Adnan",
"Gender": "Male",
"Expertise": [
{
"Skill": "PowerShell",
"Level": "7"
},
{
"Skill": "C#",
"Level": "6"
}
]
}
]
This is how we would have expected the output json string to look like. So, next time you are working with json on PowerShell, make sure to be mindful of the -Depth parameter.
No comments:
Post a Comment