Fix stringified JSON from AWS CW Log Insights

JS
S
JavaScript

Guide to parse and correct malformed JSON data extracted from AWS CloudWatch Log Insights queries, enabling proper analysis and processing.

1var stringifiedJson = `{\"obj\":{\"params\":{\"QueueUrl\":`
2
3const fixed = JSON.parse(JSON.stringify(stringifiedJson));
4
5// console.log(fixed)
6
7const cleaned = fixed
8    .replace(/\\\"/g, '"')        // Replace escaped quotes
9    .replace(/\\/g, '')           // Remove remaining backslashes
10    .replace(/"\{/g, '{')         // Fix object boundaries
11    .replace(/\}"/g, '}')         // Fix object boundaries
12    .replace(/\}\{/g, '},{');     // Fix array elements
13
14console.log(cleaned)
15// const obj = JSON.parse(fixed);
16
17// console.log('ibj', obj)
18
19// const messageBody = JSON.parse(obj.obj.params.MessageBody);
20
21
22
23

Created on 2/5/2025