47

I'm having a problem with ConvertTo-Json and was trying to understand the behavior and/or what I'm doing wrong.

Consider this sequence of commands:

$val=@{ID=10;Config=@{ID=11;Config=@{ID=12;Config='end'}}}
ConvertTo-json $val
ConvertTo-json @($val)

The first conversion gives this output:

{
    "ID":  10,
    "Config":  {
                   "ID":  11,
                   "Config":  {
                                  "ID":  12,
                                  "Config":  "end"
                              }
               }
}

The second conversion gives this output:

[
    {
        "ID":  10,
        "Config":  {
                       "ID":  11,
                       "Config":  "System.Collections.Hashtable"
                   }
    }
]

It seems that in the array case the conversion is incorrect. Any ideas on why this is happening?

JPBlanc
  • 70,406
  • 17
  • 130
  • 175
Andy Sipe
  • 473
  • 1
  • 4
  • 4

2 Answers2

83

It's a trouble with the depth, the default value is 2, can you try :

ConvertTo-json @($val) -Depth 5
JPBlanc
  • 70,406
  • 17
  • 130
  • 175
  • 2
    Nice, spent a good chunk of the day today trying to figure out why some JSON at depth 3 was coming out as `"@{name=1.0}"`. I even asked a question and everything, before finally coming across this partially on accident. – Ellesedil Aug 16 '14 at 02:28
  • Is there any way to specify "infinite depth"? – Josh Petitt Nov 07 '14 at 16:32
  • I can't find any information about that in Microsoft documentation. I never test more than 6. An "infinite depth" is theorical, do you really need more than 32. – JPBlanc Nov 07 '14 at 17:44
  • Thank you for this. This bug is extremely embarrassing. Jeez, Microsoft! – anon Feb 12 '15 at 21:12
  • 19
    IMO, the default of 2 violates the [principle of least astonishment](http://en.wikipedia.org/wiki/Principle_of_least_astonishment). The scripter wants the entire object graph to be serialized properly in most cases. Definitely a bad API design on this cmdlet. – NathanAldenSr May 14 '15 at 19:45
  • Consider [PSON](http://stackoverflow.com/questions/15139552/save-hash-table-in-powershell-object-notation-pson) – iRon May 17 '16 at 15:10
  • I agree with Nathan. If they were really concerned with hugely deep trees, then they should have made "depth" paramter mandatory. That would be better than a default of 2. – TomWardrop Sep 06 '19 at 00:15
0
-Depth $([int32]::MaxValue)

specifies infinite depth (maximum possible for ConvertTo-Json cmdlet)

YMM
  • 632
  • 1
  • 10
  • 21
  • 1
    ConvertTo-Json : The maximum depth allowed for serialization is 100. – Nick Nov 03 '16 at 14:41
  • 1
    Using larger values than needed produces totally stupid performance hits. dont even go there –  Jul 05 '17 at 08:57
  • @ConradB Just wondering... Do you know that for a fact? I assumed that would be the case as it explain why there is a Depth parameter, but have you run tests? – PhilAI Aug 01 '17 at 10:53
  • I was forced to use depth 15 on a hacked block of text I got out of the game Sunless Sea, it was terribly slow, unsure why, but in real life in workplace also found that using larger values than 20 on really long documents, of object depth ~10 took far longer, which is illogical since its' a pickle operation (maybe the document model/map is inefficient at large values). –  May 08 '18 at 08:17