PowerShell 7.3 - Issues with Exporting Nested Object Arrays to JSON Format
I keep running into I'm migrating some code and I'm sure I'm missing something obvious here, but After trying multiple solutions online, I still can't figure this out... I've been struggling with this for a few days now and could really use some help. I'm trying to export an array of nested objects to JSON format in PowerShell 7.3, but I'm running into issues with how the output is being formatted. My objects have properties that are arrays themselves, and when I use `ConvertTo-Json`, the nested arrays are not serialized correctly. Here's a snippet of my code: ```powershell $users = @( [PSCustomObject]@{ Name = 'Alice'; Roles = @('Admin', 'User'); }, [PSCustomObject]@{ Name = 'Bob'; Roles = @('User'); } ) $jsonOutput = $users | ConvertTo-Json -Depth 10 $jsonOutput | Out-File -FilePath 'C:\Users\Public\users.json' ``` When I run this, the resulting JSON file looks like this: ```json [ { "Name": "Alice", "Roles": [ "Admin", "User" ] }, { "Name": "Bob", "Roles": [ "User" ] } ] ``` While the JSON output looks correct, I noticed that when I try to read this JSON back into PowerShell using `Get-Content` and `ConvertFrom-Json`, I'm getting unexpected results: ```powershell $jsonInput = Get-Content -Path 'C:\Users\Public\users.json' | ConvertFrom-Json $jsonInput[0].Roles ``` This gives me just the array as a single string instead of a proper array object. I was expecting to get an array of strings for the `Roles` property. I've tried increasing the `-Depth` parameter, but it hasn’t resolved the issue. I also checked the data types after importing and they appear to be correctly set. Am I missing something in my export or import process? How can I ensure that the `Roles` property remains an array when read back from JSON? Any insights would be greatly appreciated! Has anyone else encountered this? My development environment is Linux. What am I doing wrong? What are your experiences with this? The stack includes Powershell and several other technologies. Any feedback is welcome! The project is a mobile app built with Powershell. I appreciate any insights!