You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ConvertTo-JSON: Nested Object of type Newtonsoft.Json.Linq.JObject not converted to Json properly #4996
Used the following Azure PS commands and discovered that a Nested JObject is just being converted to an Array instead of an Object.
In the repro steps the type of $result.Outputs.mediaServiceFullInfo.Value is of type Newtonsoft.Json.Linq.JObject as you can see in the final JSON Value doesn't have the Key and Values and has a Square Bracket around it.
$result.Outputs.mediaServiceFullInfo.Value.ToString() does output the Json Snippet under Value correctly.
Steps to reproduce
[Update] : Simplified repro steps in the comments.
I too am running in to this issue, the biggest time we run in to it is trying to generate compressed json from the output of a New-AzureRmResourceGroupDeployment call.
Here is a better formatted version of amanarneja's example
$EgJObject = New-Object -TypeName Newtonsoft.Json.Linq.JObject
$EgJObject.Add("TestValue1", "123456")
$EgJObject.Add("TestValue2", "78910")
$EgJObject.Add("TestValue3", "99999")
$dict = @{}
$dict.Add('JObject', $EgJObject)
$dict.Add('StrObject', 'This is a string Object')
$properties = @{'DictObject' = $dict; 'RandomString' = 'A quick brown fox jumped over the lazy dog'}
$object = New-Object -TypeName psobject -Property $properties
$object | ConvertTo-Json
This outputs
{
"RandomString": "A quick brown fox jumped over the lazy dog",
"DictObject": {
"JObject": [
"123456",
"78910",
"99999"
],
"StrObject": "This is a string Object"
}
}
The expected output is
{
"RandomString": "A quick brown fox jumped over the lazy dog",
"DictObject": {
"JObject": {
"TestValue1" : "123456"
"TestValue2" : "78910",
"TestValue3" : "99999"
},
"StrObject": "This is a string Object"
}
}
Also if you change the last line to $object | ConvertTo-Json -depth 4 you get the even worse output
{
"RandomString": "A quick brown fox jumped over the lazy dog",
"DictObject": {
"JObject": [
[
[
]
],
[
[
]
],
[
[
]
]
],
"StrObject": "This is a string Object"
}
}
As a work around when you import the module AzureRM.Resources it also loads Newtonsoft.Json.dll. So instead of using $result.Outputs | ConvertTo-Json you can do
Activity
SteveL-MSFT commentedon Oct 3, 2017
I had to scrub out keys from your issue. Is it possible for you to construct a simpler repro?
amanarneja commentedon Oct 4, 2017
Hey Steve
The keys were just samples and had been modified, sorry should have mentioned that.
Here's a simple Repro :
$EgJObject = New-Object -TypeName Newtonsoft.Json.Linq.JObject
$EgJObject.Add("TestValue1", "123456")
$EgJObject.Add("TestValue2", "78910")
$EgJObject.Add("TestValue3", "99999")
$dict = @{}
$dict.Add('JObject', $EgJObject)
$dict.Add('StrObject', 'This is a string Object')
$properties = @{'DictObject' = $dict;
'RandomString' = 'A quick brown fox jumped over the lazy dog'}
$object = New-Object -TypeName psobject -Property $properties
$object | ConvertTo-Json
{
"RandomString": "A quick brown fox jumped over the lazy dog",
"DictObject": {
"JObject": [
"123456",
"78910",
"99999"
],
"StrObject": "This is a string Object"
}
}
As you can see, in this case too Under JObject, we should have gotten
"JObject": {
"TestValue1" : "123456"
...
leftler commentedon Oct 4, 2017
I too am running in to this issue, the biggest time we run in to it is trying to generate compressed json from the output of a
New-AzureRmResourceGroupDeploymentcall.leftler commentedon Oct 4, 2017
Here is a better formatted version of amanarneja's example
This outputs
The expected output is
Also if you change the last line to
$object | ConvertTo-Json -depth 4you get the even worse outputleftler commentedon Oct 18, 2017
As a work around when you import the module
AzureRM.Resourcesit also loads Newtonsoft.Json.dll. So instead of using$result.Outputs | ConvertTo-Jsonyou can doWithout needing to do any additional importing of types because the library is already loaded. This will give you your expected output as a string.