Hi,
I have a problem converting some data to JSON because some objects have array properties while other only have a single property.
Let me try illustrate the problem:
# Object with object-array property inside $customProp = @() $customProp += New-Object psobject -Property @{ Name="Test1"; Size="100"} $customProp += New-Object psobject -Property @{ Name="Test2"; Size="100"} $object1 = New-Object psobject -Property @{ Name="Object1"; Something="Weird"} $final1 = $object1 | Select-Object Name, Something, @{n="Custom";e={$customProp}} # Object with object property inside $customProp = @() $customProp += New-Object psobject -Property @{ Name="Test10"; Size="200"} $object2 = New-Object psobject -Property @{ Name="Object2"; Something="Strange"} $final2 = $object2 | Select-Object Name, Something, @{n="Custom";e={$customProp}} $list = @() $list += $final1 $list += $final2 $list
The output of the above should be:
Name Something Custom ---- --------- ------ Object1 Weird {@{Name=Test1; Size=100}, @{Name=Test2; Size=100}} Object2 Strange @{Name=Test10; Size=200}
And the problem is showing already here. Object1’s Custom property has and extra set of {} around its content while Object2 is lacking those.
Resulting converting it to JSON:
$list | ConvertTo-Json -Depth 3
[ { "Name": "Object1", "Something": "Weird", "Custom": { "value": [ { "Name": "Test1", "Size": "100" }, { "Name": "Test2", "Size": "100" } ], "Count": 2 } }, { "Name": "Object2", "Something": "Strange", "Custom": { "Name": "Test10", "Size": "200" } } ]
I really need to have it to be same like this:
[ { "Name": "Object1", "Something": "Weird", "Custom": [ { "Name": "Test1", "Size": "100" }, { "Name": "Test2", "Size": "100" } ] }, { "Name": "Object2", "Something": "Strange", "Custom": [ { "Name": "Test10", "Size": "200" } ] } ]
I need to generate a C# Class from it like this:
public class Custom { public string Name { get; set; } public string Size { get; set; } } public class RootObject { public string Name { get; set; } public string Something { get; set; } public List Custom { get; set; } }
Right now json2charp.com gives me classes like this, which I do not fancy
public class Value { public string Name { get; set; } public string Size { get; set; } } public class Custom { public List value { get; set; } public int Count { get; set; } public string Name { get; set; } public string Size { get; set; } } public class RootObject { public string Name { get; set; } public string Something { get; set; } public Custom Custom { get; set; } }
Any help?