Reputation: 1203
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)
}
)
Upvotes: 3
Views: 2299
Reputation: 56
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": " "
}
Upvotes: 4