0

Description


Using ConvertTo-Json in Powershell, I am trying to convert the following object:

$richmessage = @{
    attachments = @(
        @{
            "mrkdwn_in" = @("text"; "pretext";);
            "title" = "title here";
            "title_link" = "http://somelinkhere/";
            "fallback" = "Summary of the attachment";
            "text" = "message";
            "color" = "red";       

        })      
}

write-host(ConvertTo-Json -InputObject $richmessage)

I expected to get this output:

{
    "attachments":  [
                    {
                        "text":  "message",
                        "fallback":  "Summary of the attachment",
                        "mrkdwn_in":  ["text" "pretext"],
                        "color":  "red",
                        "title":  "title here",
                        "title_link":  "http://somelinkhere/"
                    }
                ]
}

But the actual output is:

{
    "attachments":  [
                    {
                        "text":  "message",
                        "fallback":  "Summary of the attachment",
                        "mrkdwn_in":  "text pretext",
                        "color":  "red",
                        "title":  "title here",
                        "title_link":  "http://somelinkhere/"
                    }
                ]
}

Notes


  • I want the "mrkdwn_in": "text pretext" to be mrkdwn_in:["text", "pretext"]
  • If we take $richmessage = @{ "mrkdwn_in" = @("text"; "pretext"); } this will produce the array as expected, but when the array is nested like this: $richmessage = @{ attachments = @( @{"mrkdwn_in" = @("text"; "pretext"); } ) }; it concatinates the strings.
  • I'm using this to post a rich message to Slack and allow mark downs in the attachments. (see this link)

Question


How can I achieve this?

0

1 Answer 1

0

Try adding array value to the object separately

$richmessage= $richmessage["attachments"][0]["mrkdwn_in"] = @("text", "pretext");

Full example:

$richmessage = @{
    attachments = @(
        @{
            "mrkdwn_in" = ""; 
            "title" = "title here";
            "title_link" = "http://somelinkhere/";
            "fallback" = "Summary of the attachment";
            "text" = "message";
            "color" = "red";       

        })      
}
Sign up to request clarification or add additional context in comments.

4 Comments

Already tried, still gives me the same result: "mrkdwn_in" : "text pretext".
I've corrected my answer.
Thanks, I saw that, but this one is a syntax error (I'm running it on Windows PowerShell ISE). Remember this is Powershell script, not javascript or any other scripting language.
This is not correct. The issue is with depth, not formatting.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.