0

I have a JSON string I am running a replace method on, I would like to replace the second instance of a sub string In the JSON string. please see the string below.

I have two instances of this string pw.alert.timestamp.iso I would like to skip the first instance and replace the second instance.

{ "blocks": [{"type": "header","text": {"type": "plain_text","text": "{{pw.alert.severity}} - {{details}} - {{open_violations_count_critical}}"}} ], "attachments": [{"color": "{{pw.state.severityColor}}","blocks": [{"type": "section","text": {"type": "plain_text","text": "Triggered: {{pw.state.workflowId}}"}},{"type": "section","text": {"text": "Triggered:  {{metadata}}","type": "plain_text"}},{"type": "section","text": {"text": "Triggered: {{pw.alert.timestamp.iso}}","type": "plain_text"}},{"text": {"text": "{{pw.alert.description}}","type": "mrkdwn"},"type": "section"},{"type": "actions","elements": [{"url": "{{pw.alert.alertUrl}}","type": "button","value": "View Alert","action_id": "button-action-1","text": {"text": "View Alert","emoji": true,"type": "plain_text"}}]},{"text": {"type": "plain_text","text": "CORRELATIONS"},"type": "header"},{"text": {"text": "<{{pw.link.openall}} | *Open All*> | <{{pw.link.downloadall}} | *Download All*>","type": "mrkdwn"},"type": "section"},{"type": "header","text": {"type": "plain_text","emoji": true,"text": "{{pw.workflow.destinationGroup.1.name}}"}},{"type": "divider"},{"text": {"type": "mrkdwn","text": "<{{pw.workflow.destinationGroup.1.destination.0.url}} | * {{pw.workflow.destinationGroup.1.destination.0.name}}*>"},"type": "section"},{"text": {"text": "<{{pw.workflow.destinationGroup.1.destination.1.url}} | * {{pw.workflow.destinationGroup.1.destination.1.name}}*>","type": "mrkdwn"},"type": "section"},{"text": {"text": "<{{pw.workflow.destinationGroup.1.destination.2.url}} | * {{pw.workflow.destinationGroup.1.destination.2.name}}*>","type": "mrkdwn"},"type": "section"},{"text": {"type": "mrkdwn","text": "<{{pw.workflow.destinationGroup.1.destination.3.url}} | * {{pw.workflow.destinationGroup.1.destination.3.name}}*>"},"type": "section"},{"type": "section","text": {"text": "<{{pw.workflow.destinationGroup.1.destination.4.url}} | * {{pw.workflow.destinationGroup.1.destination.4.name}}*>","type": "mrkdwn"}},{"type": "section","text": {"type": "mrkdwn","text": "<{{pw.workflow.destinationGroup.1.destination.5.url}} | * {{pw.workflow.destinationGroup.1.destination.5.name}}*>"}},{"type": "header","text": {"type": "plain_text","emoji": true,"text": "{{pw.workflow.destinationGroup.2.name}}"}},{"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}}","emoji": true,"type": "plain_text"}},{"type": "divider"},{"type": "section","text": {"text": "<{{pw.workflow.destinationGroup.3.destination.0.url}} | * {{pw.workflow.destinationGroup.3.destination.0.name}}*>","type": "mrkdwn"}},{"text": {"text": "{{pw.workflow.destinationGroup.4.name}}","emoji": true,"type": "plain_text"},"type": "header"},{"type": "divider"},{"text": {"type": "mrkdwn","text": "<{{pw.workflow.destinationGroup.4.destination.0.url}} | * {{pw.workflow.destinationGroup.4.destination.0.name}}*>"},"type": "section"},{"text": {"text": "<{{pw.workflow.destinationGroup.4.destination.1.url}} | * {{pw.workflow.destinationGroup.4.destination.1.name}}*>","type": "mrkdwn"},"type": "section"},{"text": {"type": "mrkdwn","text": "<{{pw.workflow.destinationGroup.4.destination.2.url}} | * {{pw.workflow.destinationGroup.4.destination.2.name}}*>"},"type": "section"},{"text": {"text": "<{{pw.workflow.destinationGroup.4.destination.3.url}} | * {{pw.workflow.destinationGroup.4.destination.3.name}}*>","type": "mrkdwn"},"type": "section"},{"text": {"type": "plain_text","text": ": camera: Snapshots","emoji": true},"type": "header"},{"type": "divider"},{"type": "section","text": {"text": "<{{pw.workflow.destinationGroup.1.destination.0.snapshot.protectedUrl}} | *High-Resolution Image: {{pw.workflow.destinationGroup.1.destination.0.name}}*>","type": "mrkdwn"}},{"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}}","type": "image"},{"type": "section","text": {"type": "mrkdwn","text": "<{{pw.workflow.destinationGroup.1.destination.3.snapshot.protectedUrl}} | *High-Resolution Image: {{pw.workflow.destinationGroup.1.destination.3.name}}*>"}},{"type": "image","alt_text": "{{pw.workflow.destinationGroup.1.destination.3.name}}","image_url": "{{pw.workflow.destinationGroup.1.destination.3.snapshot.publicUrl}}","title": {"emoji": true,"text": "{{pw.workflow.destinationGroup.1.destination.3.name}}","type": "plain_text"}}]} ]}
4
  • I'm curious why you're doing string operations on the JSON rather than JSON.parse, and then manipulating the JavaScript object and then JSON.stringify the changed object. Commented Sep 30, 2022 at 3:36
  • No, I am extracting the JSON from an Html div, which is editable, in some cases the user may want to copy a part of the JSON in the HTML div which is a mix of JSON and HTML. Some of the divs in the JSON can be removed on click, in cases where there is more than one div with the same text I would like to replace only the second instance. I am looking for a way maybe using regex and replace to start the replace from the second occurrence. Commented Sep 30, 2022 at 3:51
  • Try this approach also shown here. Double duplicate answers for a double string replace! ;) Commented Sep 30, 2022 at 3:58
  • this worked for me, I had to reverse the matches, but it works really appreciate it thanks. ``` rawJson = rawJson.replaceAll(\{\{${html.html}\}}, match => ++i === 2 ? '' : \{\{${html.html}\}}) ``` @Wyck Commented Sep 30, 2022 at 4:22

0

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.