javascript JSON find and replace

Viewed 30

I have some JSON I would like to do a find and replace on. The issue is that other parts of the JSON might also contain that string as a part of a word. For example timestamp and pw.alert.timestamp.iso.

I only want to replace it when the match is a standalone timestamp.

"blocks": [{
  "text": {
    "text": "pw.alert.severity - pw.alert.title - timestamp",
    "type": "plain_text"
  },
  "type": "header"
}]
"color": "pw.state.severityColor",
"blocks": [{
    "text": {
      "type": "plain_text",
      "text": "Triggered: pw.alert.timestamp.iso"
    },
    "type": "section"
  },
  {
    "type": "section",
    "text": {
      "type": "mrkdwn",
      "text": "pw.alert.description"
    }
  },

Please see a sample for the JSON.

the function I am using to mod the JSON.

const handleRawInput = (htmlInput: string): string => {
    let stash = htmlInput;
    stash = stash.replace(/[\n\r]+|[\s]{2,}/g, '')
      .replace("-{{", "- {{")
      .replace(":{{", ": {{")
      .replaceAll("-", "- ")
      .replaceAll(":", ": ")
    matchKeys.current.forEach((match) => {
      const re = new RegExp(`\\b${match.key}\\b`, 'g')
      stash = !stash.includes(`\{\{${match.key}\}}`) ? stash.replace(re, `\{\{${match.key}\}}`) : stash;
    })
    return stash
  }
{
    "blocks": [
        {
            "text": {
                "type": "plain_text",
                "text": "{{pw.alert.severity}} - {{pw.alert.title}} - {{violation_callback_url}}"
            },
            "type": "header"
        }
    ],
    "attachments": [
        {
            "blocks": [
                {
                    "text": {
                        "text": "Triggered: {{pw.alert.timestamp.iso}}",
                        "type": "plain_text"
                    },
                    "type": "section"
                },
                {
                    "text": {
                        "text": "{{pw.alert.description}}",
                        "type": "mrkdwn"
                    },
                    "type": "section"
                },
                {
                    "elements": [
                        {
                            "url": "{{pw.alert.alertUrl}}",
                            "value": "View Alert",
                            "action_id": "button-  action-  1",
                            "type": "button",
                            "text": {
                                "emoji": true,
                                "type": "plain_text",
                                "text": "View Alert"
                            }
                        }
                    ],
                    "type": "actions"
                },
                {
                    "type": "header",
                    "text": {
                        "type": "plain_text",
                        "text": "CORRELATIONS"
                    }
                },
                {
                    "type": "section",
                    "text": {
                        "text": "<{{pw.link.openall}} | *Open All*> | <{{pw.link.downloadall}} | *Download All*>",
                        "type": "mrkdwn"
                    }
                },
                {
                    "type": "header",
                    "text": {
                        "type": "plain_text",
                        "text": "{{pw.workflow.destinationGroup.1.name}}",
                        "emoji": true
                    }
                },
                {
                    "type": "divider"
                },
                {
                    "text": {
                        "type": "mrkdwn",
                        "text": "<{{pw.workflow.destinationGroup.1.destination.0.url}} | *{{pw.workflow.destinationGroup.1.destination.0.name}}*>"
                    },
                    "type": "section"
                },
                {
                    "type": "section",
                    "text": {
                        "type": "mrkdwn",
                        "text": "<{{pw.workflow.destinationGroup.1.destination.1.url}} | *{{pw.workflow.destinationGroup.1.destination.1.name}}*>"
                    }
                },
                {
                    "text": {
                        "type": "mrkdwn",
                        "text": "<{{pw.workflow.destinationGroup.1.destination.2.url}} | *{{pw.workflow.destinationGroup.1.destination.2.name}}*>"
                    },
                    "type": "section"
                },
                {
                    "text": {
                        "text": "<{{pw.workflow.destinationGroup.1.destination.3.url}} | *{{pw.workflow.destinationGroup.1.destination.3.name}}*>",
                        "type": "mrkdwn"
                    },
                    "type": "section"
                },
                {
                    "text": {
                        "text": "<{{pw.workflow.destinationGroup.1.destination.4.url}} | *{{pw.workflow.destinationGroup.1.destination.4.name}}*>",
                        "type": "mrkdwn"
                    },
                    "type": "section"
                },
                {
                    "text": {
                        "emoji": true,
                        "text": "{{pw.workflow.destinationGroup.2.name}}",
                        "type": "plain_text"
                    },
                    "type": "header"
                },
                {
                    "type": "divider"
                },
                {
                    "text": {
                        "text": "<{{pw.workflow.destinationGroup.2.destination.0.url}} | *{{pw.workflow.destinationGroup.2.destination.0.name}}*>",
                        "type": "mrkdwn"
                    },
                    "type": "section"
                },
                {
                    "type": "header",
                    "text": {
                        "text": "{{pw.workflow.destinationGroup.3.name}}",
                        "type": "plain_text",
                        "emoji": true
                    }
                },
                {
                    "type": "divider"
                },
                {
                    "type": "section",
                    "text": {
                        "type": "mrkdwn",
                        "text": "<{{pw.workflow.destinationGroup.3.destination.0.url}} | *{{pw.workflow.destinationGroup.3.destination.0.name}}*>"
                    }
                },
                {
                    "type": "header",
                    "text": {
                        "text": "{{pw.workflow.destinationGroup.4.name}}",
                        "type": "plain_text",
                        "emoji": true
                    }
                },
                {
                    "type": "divider"
                },
                {
                    "text": {
                        "text": "<{{pw.workflow.destinationGroup.4.destination.0.url}} | *{{pw.workflow.destinationGroup.4.destination.0.name}}*>",
                        "type": "mrkdwn"
                    },
                    "type": "section"
                },
                {
                    "text": {
                        "type": "mrkdwn",
                        "text": "<{{pw.workflow.destinationGroup.4.destination.1.url}} | *{{pw.workflow.destinationGroup.4.destination.1.name}}*>"
                    },
                    "type": "section"
                },
                {
                    "text": {
                        "text": "<{{pw.workflow.destinationGroup.4.destination.2.url}} | *{{pw.workflow.destinationGroup.4.destination.2.name}}*>",
                        "type": "mrkdwn"
                    },
                    "type": "section"
                },
                {
                    "type": "header",
                    "text": {
                        "type": "plain_text",
                        "text": ":  camera:  Snapshots",
                        "emoji": true
                    }
                },
                {
                    "type": "divider"
                },
                {
                    "type": "section",
                    "text": {
                        "type": "mrkdwn",
                        "text": "<{{pw.workflow.destinationGroup.1.destination.0.snapshot.protectedUrl}} | *High-  Resolution Image: {{pw.workflow.destinationGroup.1.destination.0.name}}*>"
                    }
                },
                {
                    "type": "image",
                    "title": {
                        "emoji": true,
                        "type": "plain_text",
                        "text": "{{pw.workflow.destinationGroup.1.destination.0.name}}"
                    },
                    "alt_text": "{{pw.workflow.destinationGroup.1.destination.0.name}}",
                    "image_url": "{{pw.workflow.destinationGroup.1.destination.0.snapshot.publicUrl}}"
                },
                {
                    "text": {
                        "type": "mrkdwn",
                        "text": "<{{pw.workflow.destinationGroup.1.destination.3.snapshot.protectedUrl}} | *High-  Resolution Image: {{pw.workflow.destinationGroup.1.destination.3.name}}*>"
                    },
                    "type": "section"
                },
                {
                    "alt_text": "{{pw.workflow.destinationGroup.1.destination.3.name}}",
                    "title": {
                        "emoji": true,
                        "text": "{{pw.workflow.destinationGroup.1.destination.3.name}}",
                        "type": "plain_text"
                    },
                    "type": "image",
                    "image_url": "{{pw.workflow.destinationGroup.1.destination.3.snapshot.publicUrl}}"
                }
            ],
            "color": "{{pw.state.severityColor}}"
        }
    ]
}
1 Answers

I resolved the issue by running a cleanup on the JSON string after the replacement.


  const handleRawInput = (htmlInput: string): string => {
    let stash = htmlInput;
    stash = stash.replace(/[\n\r]+|[\s]{2,}/g, '')
      .replace("-{{", "- {{")
      .replace(":{{", ": {{")
      .replaceAll("-", "- ")
      .replaceAll(":", ": ")
    matchKeys.current.forEach((match) => {
      stash = !stash.includes(`\{\{${match.key}\}}`) ? stash.replace(match.key, `\{\{${match.key}\}}`) : stash;
    })
    return stash.replaceAll(".{{", ".").replaceAll("}}.", ".")
  }

Related