How do I correctly persist a Powershell object to JSON when the object was added using Add-Member to a child item? -
i'm running bit of challenge powershell code, simplified , sanitized version of shown here:
$outer = ("{""child"": {""grandchild"": {}}}" | convertfrom-json ) $inner = $outer.child.grandchild $newid = [guid]::newguid(); $nested = ("{ ""id"": ""$newid"", ""name"": ""the name goes here"" }" | convertfrom-json) $membernametoadd = "nested" $inner | add-member -membertype noteproperty -name $membernametoadd $nested $inner | convertto-json | out-file "inner.json" $outer | convertto-json | out-file "outer.json"
my specific challenge way dynamically added inner item getting persisted. output $inner (as sent inner.json) matches expect:
{ "nested": { "id": "741b6810-000e-4461-8ab8-6573e0d0b4a7", "name": "the name goes here" } }
unfortunately output $outer serialized follows:
{ "child": { "grandchild": { "nested": "@{id=741b6810-000e-4461-8ab8-6573e0d0b4a7; name=the name goes here}" } } }
clearly results in situation when serialized file (using convertfrom-json) can no longer access properties of "nested".
what have done wrong, , how can fix it?
quickly problem should come default depth
type :
$inner | convertto-json -depth 6 | out-file "inner.json" $outer | convertto-json -depth 6| out-file "outer.json"
in fact default stop parsing in depth 3, after gives tostring()
value of object.
Comments
Post a Comment