Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Building JSON POST request in powershell with an array item

Status
Not open for further replies.

blpcrs

Programmer
Aug 25, 2011
19
US
Hi all, I am new to this and not 100% sure on syntax, what I am trying to do this
Code:
$body = @"
{
   "name":"keyName[0]"
}
"@

but this seems to send all the items within keyName and [0] as the last item within the request, rather than just the first item. What am I missing? thanks in advance!
 
What are you trying to do, create an array?
Please be specific and include an example of what you're entering and what you expect to get out of the code.

## Declare the array
$array = @()

## Create an object
$New_Obj = New-Object -TypeName PSObject
$New_Obj | Add-Member -Name 'City' -MemberType Noteproperty -Value 'Dallas'
$New_Obj | Add-Member -Name 'State' -MemberType Noteproperty -Value 'Texas'
$New_Obj | Add-Member -Name 'Zip' -MemberType Noteproperty -Value '75001'

## Put the object into the array
$array += $New_Obj

## Display all the items in the array
$array | Format-Table


Light travels faster than sound. That's why some people appear bright until you hear them speak.
 
I have the array, I am trying to write the items of the array to a JSON body for a REST API Call. Note: The array in an incoming Param to the powershell script.
If the array has 3 items in it, John, Frank, Sam, and I just want John. The code below sends the name tag in JSON as "John Frank Doe [0]" rather than "name":"John"

Code:
param (
    [string]$Run = 100, 
    [parameter(Mandatory=$true,ValueFromRemainingArguments=$true)]
    [string[]]$keyName,
    [string]$FinalCase = "True"
)

.....

$body = @"
{
   "name":"keyName[0]"
}
"@ 

Invoke-RestMethod -Method POST -Uri $url -Header $dict -ContentType $contentType -Body $body
 
Figured out the syntax, thx all!
Code:
$body = @"
{
   "name":"$($keyName[2])"
}
"@
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top