3

I have a snip of code (part of a script that sends messages to Microsoft Teams) that stores some JSON in a variable:

$body = ConvertTo-Json @{
    title    = "$($messageTitle)"
    text     = " "
    sections = @(
        @{
            activityTitle    = "$($activityTitle)"
            activitySubtitle = " "
            activityImage    = "$imageLink"
        },
        @{
            title          = 'Details'
            facts          = @(
                @{
                    name  = 'Name1'
                    value = ''
                })
            potentialAction = @(@{
                    '@context' = 'http://schema.org'
                    '@type'    = 'ViewAction'
                    name       = 'Button Name'
                    target   = @("https://google.com.au")
                }                   
            )
        }
    )

}

When this code is generated, the number of 'facts' in the 'facts' section is not known. I'd like to dynamically add X number of name/value pairs into this area using a variable for example:

facts          = @(
            $facts                  
            })

In that variable have multiple name/value pairs:

@{name='name1',value='value1'},@{name='name2',value='value2'},@{name='name3',value='value3'}

However I'm struggling with the formatting, nothing I have tried so far, no combination of quotes etc has worked, the result is always invalid JSON code.

Can anyone shed so me light on the best way to insert these values into the code there?

My latest iteration is trying to convert the inserted variable $facts to JSON before inserting it:

$facts = convertto-json @(
    @{
        name  = 'name1'
        value = 'value1'
    },
    @{
        name  = 'name2'
        value = 'value2'
    },
    @{
        name  = 'name3'
        value = 'value3'
    }
)

$body = ConvertTo-Json @{
    title    = "$($messageTitle)"
    text     = " "
    sections = @(
        @{
            activityTitle    = "$($activityTitle)"
            activitySubtitle = " "
            activityImage    = "$imageLink"
        },
        @{
            title          = 'Details'
            facts          = @(
                    $facts
                )
            potentialAction = @(@{
                    '@context' = 'http://schema.org'
                    '@type'    = 'ViewAction'
                    name       = 'Button Name'
                    target   = @("https://google.com.au")
                }                   
            )
        }
    )

But the result fails, seems that no matter what I do, extra characters are being added into the variable:

{
"title":  "Test Title",
"sections":  [
                 {
                     "activitySubtitle":  " ",
                     "activityImage":  "http://icons.iconarchive.com/icons/double-j-design/origami-colored-pencil/128/green-ok-icon.png",
                     "activityTitle":  "test Activity"
                 },
                 {
                     "facts":  "[\r\n    {\r\n        \"value\":  \"value1\",\r\n        \"name\":  \"name1\"\r\n    },\r\n    {\r\n        \"value\":  \"value2\",\r\n        \"name\":  \"name2\"\r\n    },\r\n    {\r\n        \"value\":  \"value3\",\r\n        \"name\":  \"name3\"\r\n    }\r\n]",
                     "title":  "Details",
                     "potentialAction":  "System.Collections.Hashtable"
                 }
             ],
"text":  " "

}

Any help is greatly appreciated!

EDIT:

As a second example (the first one was terrible) this is attempting to use it as a normal string:

$facts = '@(
    @{
        name  = "name1"
        value = "value1"
    },
    @{
        name  = "name2"
        value = "value2"
    },
    @{
        name  = "name3"
        value = "value3"
    }
)'

$body = ConvertTo-Json @{
    title    = "$($messageTitle)"
    text     = " "
    sections = @(
        @{
            activityTitle    = "$($activityTitle)"
            activitySubtitle = " "
            activityImage    = "$imageLink"
        },
        @{
            title          = 'Details'
            facts          = $facts
            potentialAction = @(@{
                    '@context' = 'http://schema.org'
                    '@type'    = 'ViewAction'
                    name       = 'Button Name'
                    target   = @("https://google.com.au")
                }                   
            )
        }
    )

The output of which is the same, bad JSON payload.

The actual JSON output is so weridly formatted:

"facts":  "@(\r\n\t\t@{\r\n\t\t\tname  = \"name1\"\r\n\t\t\tvalue = \"value1\"\r\n\t\t},\r\n\t\t@{\r\n\t\t\tname  = \"name2\"\r\n\t\t\tvalue = \"value2\"\r\n\t\t},\r\n\t\t@{\r\n\t\t\tname  = \"name3\"\r\n\t\t\tvalue = \"value3\"\r\n\t\t}\r\n\t)",

I'm obviously just not using the correct combination of formatting techniques.

An example of what the 'facts' section should look like:

facts = @(
            @{
            name = 'Current State'
            value = $($status)
            },
            @{
            name = 'Message'
            value = $($message)
            },
            @{
            name = 'Since'
            value =$($since)
            },
            @{
            name = 'Last up'
            value = $($lastup)
            },
            @{
            name = 'Sensor'
            value = $($sensorURL)
            },
            @{
            name = 'Device'
            value = $($deviceURL)
            },
            @{
            name = 'Management URL'
            value = $($serviceURL)
            }
        )
3
  • You appear to be double-converting. In the first section, you convert the $facts to json, then in the second, you add the already json string to an object you then Convert to JSON. Commented Nov 22, 2017 at 21:38
  • @eris I just used that as the latest example, I've tried so many things now, as a string, as a hash table, as an already 'JSON'-ified string. None of them have worked so far at all. Commented Nov 22, 2017 at 21:40
  • @Eris I added another example that I have tried into the OP. It definitely appears to be an issue with formatting the string correctly when inputting into the JSON string. Commented Nov 22, 2017 at 21:44

1 Answer 1

4

Don't convert $facts to JSON, leave it as an array of hashtables,

$facts = @(
    @{
        name  = 'name1'
        value = 'value1'
    },
    @{
        name  = 'name2'
        value = 'value2'
    },
    @{
        name  = 'name3'
        value = 'value3'
    }
)

Then be sure to set a -Depth for your conversion,

$body = ConvertTo-Json @{
    title    = "$($messageTitle)"
    text     = " "
    sections = @(
        @{
            activityTitle    = "$($activityTitle)"
            activitySubtitle = " "
            activityImage    = "$imageLink"
        },
        @{
            title          = 'Details'
            facts          = $facts
            potentialAction = @(@{
                    '@context' = 'http://schema.org'
                    '@type'    = 'ViewAction'
                    name       = 'Button Name'
                    target   = @("https://google.com.au")
                }                   
            )
        }
    )
} -Depth 5

I picked 5 arbitrarily, there's no harm in over estimating on a small task like this and I was sure it was sufficient.

$body > test.json

Gives us something like so ... ?

{
    "title":  "",
    "sections":  [
                     {
                         "activitySubtitle":  " ",
                         "activityImage":  "",
                         "activityTitle":  ""
                     },
                     {
                         "facts":  [
                                       {
                                           "value":  "value1",
                                           "name":  "name1"
                                       },
                                       {
                                           "value":  "value2",
                                           "name":  "name2"
                                       },
                                       {
                                           "value":  "value3",
                                           "name":  "name3"
                                       }
                                   ],
                         "title":  "Details",
                         "potentialAction":  [
                                                 {
                                                     "@context":  "http://schema.org",
                                                     "name":  "Button Name",
                                                     "target":  [
                                                                    "https://google.com.au"
                                                                ],
                                                     "@type":  "ViewAction"
                                                 }
                                             ]
                     }
                 ],
    "text":  " "
}
Sign up to request clarification or add additional context in comments.

1 Comment

I had tried all combinations of that and more, I believe what I was missing was the -depth parameter! I just tested it this morning and it appears to be the correct! Without the -Depth parameter the 'facts' section looks like this - "facts": "System.Collections.Hashtable System.Collections.Hashtable System.Collections.Hashtable" which I mistakenly breezed over as an error, not realising it had just kept it collapsed. This could have been resolved so long ago! Thank you so much for taking the time to answer so thoroughly and helping me out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.