简体   繁体   中英

Need to convert comma separated string into json string

I will get email id's as comma separated string (recipient1@example.com,recipient2@example.com,recipient3@example.com ) . How can I build json string like this by using power shell

{"personalizations": [
{"to": [
    {"email": "recipient1@example.com"},
    {"email": "recipient2@example.com"}
]}]}

Split input string with the String.Split() method, then construct an object that looks like the desired JSON and finally pipe through ConvertTo-Json :

$recipients = "recipient1@example.com,recipient2@example.com,recipient3@example.com"

@{
  personalizations = @(
    @{
      to = @(
        $recipients.Split(',').ForEach({@{email=$_}})
      )
    }
  )
} |ConvertTo-Json -Depth 4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM