How to sanitize multiple nasty strings to JSON for Slack blocks

I am trying to make make send a Slack message using their blocks format. Problem is that I am having various nasty strings on input and the scenario keeps failing because of it. I need to strip various quotes, newlines and other things JSON does not support. The message I am trying to build is something like this, which works with simple inputs, but is very fragile. And I have several other instances of this problem in different scenarios.

~ Target JSON structure

{
	"blocks": [
		{
			"type": "header",
			"text": {
				"type": "plain_text",
				"text": ":some-emoji: New feedback from {{trim(1.`1`)}} - {{trim(1.`2`)}}.",
				"emoji": true
			}
		},
		{
			"type": "section",
			"text": {
				"type": "mrkdwn",
				"text": " 🗺️❓: More info: {{1.`3`}}\n👍: {{1.`5`}}\n🤔: {{1.`4`}}\n💬: {{1.`6`}}"
			}
		},
		{
			"type": "section",
			"text": {
				"type": "mrkdwn",
				"text": "FYI <@USR123456>, <@USR789012>."
			}
		},
		{
			"type": "section",
			"text": {
				"type": "mrkdwn",
				"text": "Available actions:"
			},
			"accessory": {
				"type": "button",
				"text": {
					"type": "plain_text",
					"text": ":other-emoji: open feedback portal (row {{1.`__ROW_NUMBER__`}})",
					"emoji": true
				},
				"url": "https://some-url.com",
				"action_id": "button-action"
			}
		},
		{
			"type": "divider"
		}
	]
}

I’ve looked through docs and have been Googling this since yesterday and the best practical approach seems to be to chain several replace functions and maybe a RegExp which is way too tedious work for such a simple task. Feels like I must be missing something.

What I have already tried

  • Using chained replaces feels wrong and the readability is bad.
"blocks": [
…
    {
        "type": "section",
        "text": {
            "type": "mrkdwn",
            "text": " 🗺️❓: Location, {{replace(1.`3`; \"\\n\"; \" \")}}\\n👍: {{replace(replace(replace(1.`5`; \"\\\"\"; \"\"); \"\\n\"; \" \"); \"'\"; \"'\")}}\\n🤔: {{replace(replace(replace(1.`4`; \"\\\"\"; \"\"); \"\\n\"; \" \"); \"'\"; \"'\")}}\\n💬: {{replace(replace(replace(1.`6`; \"\\\"\"; \"\"); \"\\n\"; \" \"); \"'\"; \"'\")}}"
        }
    },
…
  • Composing a string and transforming to JSON does not seem to work, the output is like this:
"{\n\t\"blocks\": [\n\t\t{\n\t\t\t\"type\": \"header\",\n\t\t\t\"text\": {\n\t\t\t\t\"type\": \"plain_text\",\n\t\t\t\t\"text\": \":emoji: New feedback from somebody@example.com.\",\n\t\t\t\t\"emoji\": true\n\t\t\t}\n\t\t},\n\t\t{\n\t\t\t\"type\": \"section\",\n\t\t\t\"text\": {\n\t\t\t\t\"type\": \"mrkdwn\",\n\t\t\t\t\"text\": \" 

Please tell me what is the best way to deal with this. Thank you!

Hi @Tomas_Biheler

Did you try transforming to JSON only the text variable and adding the transformed JSON to your payload?

Looks like you have too many text variables. A “better” module to use would be the “Create JSON” module, otherwise you will need one “Transform to JSON” module for each variable.

Hope this helps! Let me know if there are any further questions or issues.

— @samliew

P.S.: Investing some effort into the Make Academy will save you lots of time and frustration using Make.

1 Like

Thanks for the suggestions @samliew, @ponvaskon ! It was however not what I was looking for.

What I actually needed was to take a few steps back and now I return with a solution.

  1. Google Sheet module - Many variables data source
  2. Parse JSON module - Will correctly map variables into the final JSON object. Do not use “data structure” only use JSON string. Run the scenario with sample data to be able to populate last module.
  3. Slack module - Will accept (bundle) output from module 2. Seems to be proper sanitized JSON.

See screenshots. Result is nicely formatted Slack blocks message without dealing with sluggish 1700vh high “data structure” editor.


2 Likes