I am trying to read an SNS Topic that I stored into a file to query the key values. I keep getting an error:
`testRegex.ts:8:45 - error TS2339: Property 'Records' does not exist on type 'string'.
8 const messageText = JSON.parse(data.Records[0].Sns.Message.message);`
The value in the file DOES exist, but something in the readFile() Module is not allowing the string to be converted in JSON.parse().
Here is the code:
import { readFileSync, promises as fsPromises, readFile } from "fs";
readFile("./regex", "utf-8", (err, data) => {
if (err) {
console.error(err);
return;
}
const messageText = JSON.parse(data.Records[0].Sns.Message.message);
console.log(messageText);
});
Data file format and content (regex):
"Records": [
{
"EventSource": "aws:sns",
"EventVersion": "1.0",
"EventSubscriptionArn": "arn:aws:sns:us-west-2:123456789012:Opsgenie_Alert_Messages_Prod:11g22y11-dd9e-4e40-a17c-d1111111111",
"Sns": {
"Type": "Notification",
"MessageId": "5885272a-55d5-5c91-bf15-5b0253cab710",
"TopicArn": "arn:aws:sns:us-west-2:123456789012:Opsgenie_Alert_Messages_Prod",
"Subject": null,
"Message": "{\"integrationName\":\"AmazonSNS (Outgoing)\",\"alert\":{\"alertId\":\"182374692817346129837\",\"message\":\"[AS] foobar_TEST on 10.00.0.00 has late Task Agent Jobs on TSTSRVR - Notification 8.9.3.11\",\"tags\":[\"alerts_as8\"],\"tinyId\":\"20000\",\"source\":\"35.00.000.0\",\"entity\":\"\",\"alias\":\"0baba525-5c10-41f0-b480-1293c2900c53_late_120\",\"createdAt\":1672348639658,\"updatedAt\":1672348640358000000,\"username\":\"Alert API\",\"userFullName\":\"Alert API\",\"description\":\"Last Status Message (from DB):\\nProcessing queue subscription...\\n\\nScheduled Start Time:\\n2022-12-29T08:00:00\\n\\nLast Heartbeat Time:\\n2022-12-29T10:00:00.293\\n\\nHelpful links: \\n[Graylog](http://graylog.companyname.local/)\\n\\nThese links rely on naming conventions and might be incorrect:\\n\\n[Log File](http://graylog.companyname.local/search?q=institution:fakesiteuri_TEST AND TA_Log_File:Notification\\2022\-12%5=%%C-29\-08_00_23.log&rangetype=relative&relative=17777)\\n\\n[Site]( https://www.companyname.com/some_TEST/Logon.aspx?nosso= )\\n\\n\\n{AgentURL=taskagent://TSTTSK50:7640,Version=8.9.3.11, Database=DB_TESTsites\":{\"AgentURL\":\"taskagent://TSTTSK50:7640\",\"AstraVersion\":\"8.9.3.11\",\"Database\":\"l}}",
"Timestamp": "2022-12-29T21:17:19.913Z",
"SignatureVersion": "1",
"Signature": "<SomeValueHere>",
"SigningCertUrl": "https:*.pem",
"UnsubscribeUrl": "https://sns.us-west-2.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-west-:Opsgenie_Alert_Messages_Prod:",
"MessageAttributes": {}
}
}
]
I tried querying the object in the first node (data) and it prints out. When I try to add the second node (Records[0]) it errors out on me.
I expect to be able to query the object with JSON.parse() and store the values into a variable.
CodePudding user response:
Your code fails even before trying to parse the JSON.
How about something like this?
import { readFileSync, promises as fsPromises, readFile } from 'fs';
readFile('./regex', 'utf-8', (err, data) => {
if(err) {
console.error(err);
} else {
const parsedData = JSON.parse(data);
const messageText = parsedData.Records[0].Sns.Message.message;
console.log(messageText);
}
});
CodePudding user response:
Thanks to Matj and Ol234 for putting me on the right track!
I was missing the step to parse the outer JSON String (regex file) prior to parsing the inner JSON String (Records[0].Sns.Message
).
Here is the working code:
import { readFileSync, promises as fsPromises, readFile } from 'fs';
readFile('./regex', 'utf-8', (err, data) => {
if (err) {
console.error(err);
return;
}
// Parse outer/parent JSON String
const rawData = JSON.parse(data);
//Parse Inner JSON String "Message"
const messageJson = JSON.parse(rawData.Records[0].Sns.Message);
//Log value of tinyId to console
console.log(messageJson.alert.tinyId);
});