# List to JSON for sending SMS

**URL:** <https://forums.powershell.org/t/list-to-json-for-sending-sms/15469>\
**Category:** PowerShell Help\
**Created:** [January 10, 2021, 10:47pm UTC](https://forums.powershell.org/t/list-to-json-for-sending-sms/15469 "2021-01-10T22:47:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jdobbsy1987](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/jdobbsy1987/32/23_2.png) [@jdobbsy1987](https://forums.powershell.org/u/jdobbsy1987)\
**Post date:** [January 10, 2021, 10:47pm UTC](https://forums.powershell.org/t/list-to-json-for-sending-sms/15469/1 "2021-01-10T22:47:58Z")

</div>

Hi,

I’m trying to add an SMS alert to a script using ClickSend however having an issue with getting powershell to output the required format in JSON i think, i get a response saying “The remote server returned an error: (400) Bad Request”

I suspect the issue is me not correctly formating the body of the message inline with their docs here - [ClickSend v3 API](https://developers.clicksend.com/docs/rest/v3/#ClickSend-v3-API-SMS)

&nbsp;

```
$pwd = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f 'username', 'password')))

$body = @{
	messages = @{
		from = 'sender'
		to = '+447800000000'
		body = 'SMS message body'
	}
	
}

$json = ConvertTo-Json -InputObject $body

$smsresponse = Invoke-RestMethod -Headers @{ Authorization = ("Basic {0}" -f $pwd) } -uri 'https://rest.clicksend.com/v3/sms/send' -Method Post -Body $json -ContentType 'application/json'
```

&nbsp;

Any help adjusting my script would be much appreciated.

Regards,

Jamie

---

<div class="post-metadata">

**Author:** ![ralphmwr](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ralphmwr/32/1064_2.png) [@ralphmwr](https://forums.powershell.org/u/ralphmwr)\
**Post date:** [January 11, 2021, 6:34am UTC](https://forums.powershell.org/t/list-to-json-for-sending-sms/15469/2 "2021-01-11T06:34:54Z")

</div>

I have zero experience with this API, but all the examples I saw in the docs show the message property as an array. That is the only difference I see in your json. You might want to try that and see if it is the issue. Pretty simple to do, just wrap your messages value in an array subexpression @().

```
$body = @{
 messages = @(@{
 from = 'sender'
 to = '+447800000000'
 body = 'SMS message body'
 })
 
}
 
 
$json = ConvertTo-Json -InputObject $body
```

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![jdobbsy1987](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/jdobbsy1987/32/23_2.png) [@jdobbsy1987](https://forums.powershell.org/u/jdobbsy1987)\
**Post date:** [January 11, 2021, 9:12am UTC](https://forums.powershell.org/t/list-to-json-for-sending-sms/15469/3 "2021-01-11T09:12:47Z")

</div>

Thank you so much, that worked!

I can’t believe how close my code was all this time, i actually swapped @{ for @( and played with different variations of that etc but never tried them together, although i now see whats happening and how that works so thank you very much.

&nbsp;

- Jamie

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:28pm UTC](https://forums.powershell.org/t/list-to-json-for-sending-sms/15469/4 "2024-05-16T20:28:03Z")

</div>

Closed on May 16, 2024, 8:28pm UTC


