javascript - 将日志文件文本解析为 JSON
问题描述
我正在尝试创建一个将日志文件解析为 JSON 文件以供将来处理的过程。
日志文件具有以下格式:
[
{
"textPayload": "Dialogues0 gRPC Response : response_id: \"fc4e2e63-509b4ae2-a7d8-c401e563aa4b\"\nquery_result {\n query_text: \"wonderful\"\naction: \"smalltalk.appraisal.good\"\n parameters {\n }\nall_required_params_present: true\n fulfillment_text: \"Glad you think so!\"\n fulfillment_messages {\n text {\n text: \"Glad you think so!\"\n }\n }\n intent_detection_confidence: 1.0\n language_code: \"en\"\nsentiment_analysis_result {\n query_text_sentiment {\n }\n}\n}\nagent_id: \"3d22af45-f603-4a8a-a7ce-a9b2af47b762\"\n",
"insertId": "1lfux63g16s1nna",
"resource": {
"type": "global",
"labels": {
"project_id": "data-analytics-digital-dev"
}
},
"timestamp": "2018-11-07T14:31:02.435Z",
"severity": "INFO",
"labels": {
"request_id": "fc4e2e63-509b-4ae2-a7d8-c401e563aa4b",
"type": "dialogflow_response",
"protocol": "V2BETA1"
},
"logName": "projects/data-analytics-digital-dev/logs/dialogflow_agent",
"trace": "7fa08c8c-df50-4d46-9f20-b1e357b844a4",
"receiveTimestamp": "2018-11-07T14:31:02.555590714Z"
}
]
我的目标是“textPayload”的内容
Node.js 代码是:
fs = require('fs');
fs.readFile('./global_logs1.json', 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
let parsedLog = JSON.parse(data);
for(let myKey in parsedLog) {
let tempJson = (parsedLog[myKey]["textPayload"]);
tempJson = (tempJson.substr(91,));
tempJson = (tempJson.substr(0, tempJson.length - 50));
console.log((tempJson));
//console.log(JSON.parse(tempJson));
}
});
它会产生一个类似 JSON 的字符串:
{
query_text: "wonderful"
action: "smalltalk.appraisal.good"
parameters {
}
all_required_params_present: true
fulfillment_text: "Glad you think so!"
fulfillment_messages {
text {
text: "Glad you think so!"
}
}
intent_detection_confidence: 1.0
language_code: "en"
sentiment_analysis_result {
query_text_sentiment {
}
}
}
但是,当我调用 JSON.parser 时,我收到一个错误:
undefined:2
query_text: "wonderful"
^
SyntaxError: Unexpected token q in JSON at position 5
at JSON.parse (<anonymous>)
at c:\Codes\Logging\test.js:15:26
at FSReqWrap.readFileAfterClose [as oncomplete]
(internal/fs/read_file_context.js:53:3)
似乎它缺少双引号,但我不确定。
关于如何做的任何想法?
解决方案
“textPayload”属性中的文本绝对不是有效的 JSON。正如您所指出的,属性名称缺少双引号。您将需要自己解析它。您可以尝试使用正则表达式(结果和意见可能会有所不同。)或使用不使用 eval 的现有“宽松 JSON”库。
编辑:使用模块'relaxed-json'我把这个脏脚本放在一起。您显然可以自己清理它而不是依赖外部模块,但我在这里很懒,甚至可能有一个开箱即用的解决方案来更好地清理它,但这很有效。您需要删除换行符,在项目后添加逗号,在对象属性中添加冒号并在属性名称中添加双引号,以使其成为有效的 JSON。就像我说的,它是一个肮脏的脚本,我做了一些糟糕的正则表达式匹配和替换,只是为了做一个概念证明,所以我准备好接受它有多糟糕的评判。
var parsed = require('./payload.json');
const rjson = require('relaxed-json');
for(let key in parsed){
let tempJson = (parsed[key]["textPayload"]);
tempJson = (tempJson.substr(91,));
tempJson = (tempJson.substr(0, tempJson.length - 50));
tempJson = tempJson.replace(/\n/g,",");
tempJson = tempJson.replace(/\{,/g,"{");
tempJson = tempJson.replace(/ \{/g,":{");
let transformed = JSON.parse(rjson.transform(tempJson));
console.log(transformed);
}
输出现在是一个真正的 javascript 对象。
{ query_text: 'wonderful',
action: 'smalltalk.appraisal.good',
parameters: {},
all_required_params_present: true,
fulfillment_text: 'Glad you think so!',
fulfillment_messages: { text: { text: 'Glad you think so!' } },
intent_detection_confidence: 1,
language_code: 'en',
sentiment_analysis_result: { query_text_sentiment: {} } }
推荐阅读
- javascript - 从谷歌地方提取数据 api address_components
- sql - sql查询到pl sql过程
- reactjs - Motion-Frame 与 Intersection Observer map() 并将 refs 作为道具传递
- java - log4j 在 .nfsXXXX 而不是 .log 文件中打印日志
- ios - 为什么我不能用 AVPlayer 播放某些 .m3u8 项目?
- c# - 如何确定用户输入的格式是否为 5,3,然后我需要将其转换为 5.3,反之亦然,然后在 dotnet 中执行计算
- singlestore - 在 Mem SQL 中按顺序排列为 Null
- javascript - 如何在 Node.js 上流畅的 FFmpeg 编辑中在 React JS 中制作进度条?
- javascript - 从 VBA 到 JS 的转换 - 错误的输出
- javascript - HTML5 选项卡,包括表单和提交按钮