openlang

Making arrays with ConvertTo-Json

Hi,

I'm building JSON to send in an HTTP API call, and one aspect of the syntax has me stuck.

Here's the code:

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

Am I right that defining a hash table and then turning that into JSON is the correct approach? As a Powershell newcomer, I want to avoid picking up bad habits while teaching myself.

What I specifically need is an array for parameter under two circumstances:

If there's actually more than one name attribute.

If there's just one, like here, but the API expects that value to be represented as a json array.

I've attempted a few approaches without success, so I'd welcome any guidence on getting this right.

Cheers

The conversation continues 2

Follow the reasoning
  1. Permalink

    Arrays are valid hashtable values, so specify one this way:

    $payload = @{
      resourceType = 'Parameters'
      parameter = @(
        @{
          name = 'ValueSet'
        }
      )
    }

Other sides of the same question

  1. Missing field through a derived table in SQL

    I'm executing the following SQL statement: It produces the following error: #1054 - Unknown column 'shipping.post_id' in 'on…

    Replies 2Latest activity sql
  2. Testing D arrays for a matching entry

    with associative arrays this is possible how is this done with ordinary arrays? I'd like a validation expression, for example

    Replies 2Latest activity d
  3. Removing an optional column safely with PostgreSQL 9+

    I want to remove a column belonging to a table. What's the way to determine whether that column is present? I read the manual…

    Replies 2Latest activity sql