question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ConvertTo-JSON should allow nesting 0

See original GitHub issue

There seems to be an oversight of what ConvertTo-JSON allows when it comes to nesting. Since the default value is 2 (not 0) it’s impossible to set it to 0, as minimal value is 1.

Steps to reproduce

[PSCustomObject] @{
    Test  = 1
    Test2 = 2
    Test3 = [PSCustomObject] @{
        IdontWantThat = 1
    }
} | ConvertTo-Json -Depth 0

Expected behavior

[PSCustomObject] @{
    Test  = 1
    Test2 = 2
    Test3 = [PSCustomObject] @{
        IdontWantThat = 1
    }
} | ConvertTo-Json -Depth 0

Should be:

{
"Test":"1",
"Test2":"2",
"Test3":"@{IDontWantThat=1}"}

or similar

Actual behavior

[PSCustomObject] @{
    Test  = 1
    Test2 = 2
    Test3 = [PSCustomObject] @{
        IdontWantThat = 1
    }
} | ConvertTo-Json -Depth 0
ConvertTo-Json : Cannot validate argument on parameter 'Depth'. The 0 argument is less than the minimum allowed range of 1. Supply an argument that is greater than or equal to 1 and then try the command again.

Environment data


Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:19 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
PrzemyslawKlyscommented, Mar 23, 2021

@TravisEz13 You already allow setting depth. You also specify it to a default value. I want to be able to choose that. Does Depth 2 for an object that is nested 5 levels deep is any different than what I requested?

I already showed you the HTML example above. I have a PowerShell module called PSWriteHTML. By default, it uses ConvertTo-HTML which uses depth 0. However, pushing 50k users into HTML makes browsers cry when trying to show it. So I’m using the javascript approach of storing data which prevents autoloading of all 50k rows before displaying them.

image

Since JavaScript is very similar to JSON I’m using ConverTo-JSON to store it within an HTML file. I only require flat JSON. While I guess I could store 5 or 10 or 50 levels deep in there it doesn’t make sense for me as I want to display it as a Table (so flat storage). Now imagine 50k adobjects objects each having 5 levels deep of which 4 are useless to display. While the display would still work, the size of the generated HTML would go from 80MB to 120 or 160MB in size. I don’t need that.

This is just one use case. A similar use case is used within the PSTeams Powershell module. I sometimes don’t want to get 2 levels deep when I require just 0.

Since it takes ages to implement this little change I wrote my own ConvertTo-JSONLiteral which happens to work in PS5+ and allows me to also define how Arrays are treated (for example join them with defined char), how DateTime is converted - for example to a specific format, how numbers are treated, how enums are treated. I got a greater control for display purposes.

While you specifically are thinking JSON use case is very limited I’ve found that JSON is really similar to JavaScript or CSS with some exceptions or small changes and I use it frequently in ways you would not expect. I don’t care if I lose data, as long as I am doing this explicitly.

This change just adds the ability to go from 0 to 100, not from 1-2 to 100. It’s trivial, and just fixes something that should have been there from the beginning. The bigger problem is going to remove the default Depth which is set to 2 now, but it’s outside of the scope of this change.

1reaction
kvprasooncommented, Sep 25, 2020

Depth 0 with code change has

{
  "Test": 1, 
  "Test2": 2,
  "Test3": "@{IdontWantThat=1}"
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Powershell ConvertTo-JSON missing nested level
1 Answer 1 ... Specifies how many levels of contained objects are included in the JSON representation. The default value is 2. Set...
Read more >
Expressions - Azure Pipelines
An expression can be a literal, a reference to a variable, a reference to a dependency, a function, or a valid nested combination...
Read more >
ConvertTo-Json - PowerShell
Specifies how many levels of contained objects are included in the JSON representation. The value can be any number from 0 to 100...
Read more >
Unable to ConvertTo-Json without -Depth parameter ...
So I'm trying to do a simple: Get-Process | ConvertTo-Json | Out-File procs.json ... all nested elements in the above JSON will be...
Read more >
Working with a nested dictionary - Pathfinder Community
To convert the nested dictionary to a JSON string, you can use the "Convert to JSON" action from the "JSON" package in Automation...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found