Convert-ToJSON questions

Hello,

I’m working on creating JSON, for a HTTP API request, and I can’t figure out part of the syntax.

My code is:

$body = [ordered]@{  
  resourceType='Parameters';
  parameter=@{
    name='Valueset'
  }
}
write-host ($body|ConvertTo-JSON)

I think my approach is correct in that I’m creating a hash table and converting it to JSON? I’m new to Powershell and trying to make sure I’m not teaching myself incorrect ways of doing things.

The specific problem I have is that I need to make parameter an array in two situations:

When I actually have more than one name attribute.
When, as above, I only have one but I need it to be a json array as that’s what the API server is expecting.

I’ve tried several different methods and not been able to get it to work so any advise/guidance would be appreciated.

Thanks

1 Like

A hashtable can contain array, just declare it like this:

$body = @{  
    resourceType = 'Parameters'
    parameter    = @(
        @{
            name = 'ValueSet'
        }
    )
}
1 Like

Thank you.

Now I understand the syntax and it solved the problem.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.