我正在尝试创建一个将日志文件解析为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)
似乎缺少双引号,但我不确定。
有什么想法吗?
答案 0 :(得分:0)
“ 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: {} } }
答案 1 :(得分:0)
您的tempJson
实际上是一个javascript对象。
要将其转换回json,只需调用
JSON.stringify(tempJson)
有时这两个概念混合使用,因为它们很容易互换。