0

I am trying to send a JSON api request via Powershell script, but nested hashtables (ie. anything more than one level deep) will only render as 'System.Collections.Hashtable' instead of the actual content. For example, if I have a structure like this:

@{
   Name = "John Smith";
   Age = 28;
   siblings = @(
     @{
        Name = "Jane Smith";
        Age = 33;
      },
      @{
         Name = "Bill Smith";
         Age = 27;
       }
   )
 }

When I convert to json to send to an application/json rest api, the inner hashtables don't render as the name and age of the siblings, but just as 'System.Collections.Hashtable'. I have not encountered an issue like this with any other language. If I use the function ConvertTo-Json on these inner hashtables, they are rendered as strings instead of json. How do I get nested hashtables to properly render the same way the first level outer hashtables render? I have checked online and I haven't found any solutions that work.

EDIT: I fixed the syntax issues. I will try out your fixes and see if that helps, then respond to your comments.

Joel Joel Binks
  • 1,628
  • 5
  • 27
  • 47
  • 1
    First, the example has syntax mistake, powershell hashtable uses `=` instead of `:`, secondly, I don't have any trouble `convertto-json` and `convertfrom-json`, the nested hashtable works fine, it's `psobject` finally but not `string` as I tested. Better put your full codes to help identify what's wrong. – Larry Song Jun 05 '19 at 00:18
  • 2
    Use the `-Depth` parameter when converting to JSON to avoid that issue. – TheMadTechnician Jun 05 '19 at 00:39
  • Thanks to both of you for your comments. It was simply a matter of setting the proper -Depth level. – Joel Joel Binks Jun 05 '19 at 18:15

0 Answers0