I've seen a number of posts on this but can't figure out what I need exactly. I've tried -r
and argjson
among other things.
I need the newlines to remain as \n
and not be escaped to \\n
.
I'd also like to be able to use ``` for code blocks but it ignores that section of the string.
FALLBACK_MESSAGE="TEST MESSAGE - $HOSTNAME"
MARKDOWN_MESSAGE="TEST MESSAGE - $HOSTNAME \(0x0a) \(\n) Hi <@U12345789>\n```Can we do a\nmultiline code block```"
JSON_STRING=$( jq -nr \
--arg fallbackMessage "$FALLBACK_MESSAGE" \
--arg sectionType "section" \
--arg markdownType "mrkdwn" \
--arg textMessage "$MARKDOWN_MESSAGE" \
'{
text: $fallbackMessage,
blocks: [
{
type: $sectionType,
text: {
type: $markdownType,
text: $textMessage
}
}
]
}')
echo $JSON_STRING
Outputs:
{ "text": "TEST MESSAGE - devDebug", "blocks": [ { "type": "section", "text": { "type": "mrkdwn", "text": "TEST MESSAGE - devDebug \\(0x0a) \\(\\n) Hi <@U12345789>\\n" } } ] }
n
. – Hellernewline=$'\n'
, orprintf -v newline '%b' '\n'
will make a shell variablenewline
with an actual newline in it. – Heller\n
is"\\n"
, whereas the JSON form of a string with only a newline is"\n"
. So you want the data you're putting into jq to have the literal newline that's ready to be converted, not the two-character\n
sequence that isn't. – Heller