【发布时间】:2021-02-21 02:13:18
【问题描述】:
源数据:
Dialogflow Response : id: \"9f912860-e5c6-4e57-934b-8639595b947f-d571b79b\"\nlang: \"en\"\nsession_id: \"3e22f813-79bb-b29a-1def-4c5e4ce4b23b\"\ntimestamp: \"2020-11-02T12:38:09.875Z\"\nresult {\n source: \"agent\"\n resolved_query: \"hi\"\n action: \"input.welcome\"\n score: 1.0\n parameters {\n }\n contexts {\n name: \"awaits_choice\"\n lifespan: 5\n parameters {\n fields {\n key: \"accountNo\"\n value {\n number_value: 2.1012875E7\n }\n }\n fields {\n key: \"accountNo.original\"\n value {\n string_value: \"21012875\"\n }\n }\n }\n }\n contexts {\n name: \"accountno\"\n lifespan: 49\n parameters {\n fields {\n key: \"accountNo\"\n value {\n number_value: 2.1012875E7\n }\n }\n fields {\n key: \"accountNo.original\"\n value {\n string_value: \"21012875\"\n }\n }\n }\n }\n metadata {\n intent_id: \"5bb77027-405a-401e-ac3f-497c471359c8\"\n intent_name: \"Default Welcome Intent\"\n webhook_used: \"false\"\n webhook_for_slot_filling_used: \"false\"\n is_fallback_intent: \"false\"\n }\n fulfillment {\n speech: \"Hi, I’m Phoenix from Central Huson. Thanks for reaching out to me. I can answer all your questions and provide the right information you are seeking.\"\n messages {\n platform: \"google\"\n display_text: \"Hi, I’m Phoenix from Central Huson. Thanks for reaching out to me. I can answer all your questions and provide the right information you are seeking.\"\n text_to_speech: \"Hi, I’m Patrick from The MDC. For all emergencies, Please call 9-1-1 immediately! Or call us on 860-278-7850. You can ask me any question. Are you an existing customer?\"\n lang: \"en\"\n type {\n string_value: \"simple_response\"\n }\n }\n messages {\n platform: \"telephony\"\n lang: \"en\"\n type {\n number_value: 14.0\n }\n text: \"Hi, I’m Patrick from The MDC. For all emergencies, Please call 9-1-1 immediately! Or call us on 860-278-7850. So, are you existing customer?\"\n }\n messages {\n lang: \"en\"\n type {\n number_value: 0.0\n }\n speech {\n string_value: \"Hi, I’m Phoenix from Central Huson. Thanks for reaching out to me. I can answer all your questions and provide the right information you are seeking.\"\n }\n }\n messages {\n payload {\n fields {\n key: \"richContent\"\n value {\n list_value {\n values {\n list_value {\n values {\n struct_value {\n fields {\n key: \"image\"\n value {\n struct_value {\n fields {\n key: \"src\"\n value {\n struct_value {\n fields {\n key: \"rawUrl\"\n value {\n string_value: \"https://example.com/images/logo.png\"\n }\n }\n }\n }\n }\n }\n }\n }\n fields {\n key: \"subtitle\"\n value {\n string_value: \"Please call us on 18009428274 or 911\"\n }\n }\n fields {\n key: \"text\"\n value {\n string_value: \"<b> Stop </b> what you are doing <br> <b> Go </b> outside immediately <br> <b> Let us know </b> After making your report, a representative will come to your location and check for potential leaks or faulty appliances.\"\n }\n }\n fields {\n key: \"title\"\n value {\n string_value: \"Emergency\"\n }\n }\n fields {\n key: \"type\"\n value {\n string_value: \"accordion\"\n }\n }\n }\n }\n }\n }\n }\n }\n }\n }\n lang: \"en\"\n type {\n number_value: 4.0\n }\n }\n messages {\n lang: \"en\"\n type {\n number_value: 0.0\n }\n speech {\n string_value: \"How can I help you today?\"\n }\n }\n messages {\n payload {\n fields {\n key: \"richContent\"\n value {\n list_value {\n values {\n list_value {\n values {\n struct_value {\n fields {\n key: \"options\"\n value {\n list_value {\n values {\n struct_value {\n fields {\n key: \"text\"\n value {\n string_value: \"I am existing customer needs support\"\n }\n }\n }\n }\n values {\n struct_value {\n fields {\n key: \"text\"\n value {\n string_value: \"Just browsing\"\n }\n }\n }\n }\n values {\n struct_value {\n fields {\n key: \"text\"\n value {\n string_value: \"Ask me anything \"\n }\n }\n }\n }\n }\n }\n }\n fields {\n key: \"type\"\n value {\n string_value: \"chips\"\n }\n }\n }\n }\n }\n }\n }\n }\n }\n }\n lang: \"en\"\n type {\n number_value: 4.0\n }\n }\n }\n}\nstatus {\n code: 200\n error_type: \"success\"\n}\n
预期输出:
{
"id": "6ae5f723-538f-4df8-ad9a-88943b28484d-ee7586fb",
"lang": "en",
"session_id": "dfMessenger-12848231",
"timestamp": "2020-11-06T06:46:50.794Z",
"result": {
"source": "agent",
"resolved_query": "WELCOME",
"action": "input.welcome",
"score": 1,
"parameters": {},
"contexts": {
"name": "awaits_choice",
"lifespan": 5,
"parameters": {}
},
"metadata": {
"intent_id": "5bb77027-405a-401e-ac3f-497c471359c8",
"intent_name": "Default Welcome Intent",
"webhook_used": "false",
"webhook_for_slot_filling_used": "false",
"is_fallback_intent": "false"
},
"fulfillment": {
"speech": "Hi, I’m sathish",
"messages": {
"payload": {
"fields": {
"key": "richContent",
"value": {
"list_value": {
"values": {
"list_value": {
"values": {
"struct_value": {
"fields": {
"key": "type",
"value": {
"string_value": "chips"
}
}
}
}
}
}
}
}
}
},
"lang": "en",
"type": {
"number_value": 4
}
}
}
},
"status": {
"code": 200,
"error_type": "success"
}
}
以上是我的字符串,我想用node.js形成一个有效的json字符串。 有人可以帮我形成json字符串。 提前致谢。 我尝试使用正则表达式替换但对我不起作用。 我已经浪费了两天时间来形成一个完美的 json 来显示这些值。 我非常需要有人帮助我解决这个问题。
源代码:
const fs = require('fs');
let rawdata = `Dialogflow Response : id: \"9f912860-e5c6-4e57-934b-8639595b947f-d571b79b\"\nlang: \"en\"\nsession_id: \"3e22f813-79bb-b29a-1def-4c5e4ce4b23b\"\ntimestamp: \"2020-11-02T12:38:09.875Z\"\nresult {\n source: \"agent\"\n resolved_query: \"hi\"\n action: \"input.welcome\"\n score: 1.0\n parameters {\n }\n contexts {\n name: \"awaits_choice\"\n lifespan: 5\n parameters {\n fields {\n key: \"accountNo\"\n value {\n number_value: 2.1012875E7\n }\n }\n fields {\n key: \"accountNo.original\"\n value {\n string_value: \"21012875\"\n }\n }\n }\n }\n contexts {\n name: \"accountno\"\n lifespan: 49\n parameters {\n fields {\n key: \"accountNo\"\n value {\n number_value: 2.1012875E7\n }\n }\n fields {\n key: \"accountNo.original\"\n value {\n string_value: \"21012875\"\n }\n }\n }\n }\n metadata {\n intent_id: \"5bb77027-405a-401e-ac3f-497c471359c8\"\n intent_name: \"Default Welcome Intent\"\n webhook_used: \"false\"\n webhook_for_slot_filling_used: \"false\"\n is_fallback_intent: \"false\"\n }\n fulfillment {\n speech: \"Hi, I’m Phoenix from Central Huson. Thanks for reaching out to me. I can answer all your questions and provide the right information you are seeking.\"\n messages {\n platform: \"google\"\n display_text: \"Hi, I’m Phoenix from Central Huson. Thanks for reaching out to me. I can answer all your questions and provide the right information you are seeking.\"\n text_to_speech: \"Hi, I’m Patrick from The MDC. For all emergencies, Please call 9-1-1 immediately! Or call us on 860-278-7850. You can ask me any question. Are you an existing customer?\"\n lang: \"en\"\n type {\n string_value: \"simple_response\"\n }\n }\n messages {\n platform: \"telephony\"\n lang: \"en\"\n type {\n number_value: 14.0\n }\n text: \"Hi, I’m Patrick from The MDC. For all emergencies, Please call 9-1-1 immediately! Or call us on 860-278-7850. So, are you existing customer?\"\n }\n messages {\n lang: \"en\"\n type {\n number_value: 0.0\n }\n speech {\n string_value: \"Hi, I’m Phoenix from Central Huson. Thanks for reaching out to me. I can answer all your questions and provide the right information you are seeking.\"\n }\n }\n messages {\n payload {\n fields {\n key: \"richContent\"\n value {\n list_value {\n values {\n list_value {\n values {\n struct_value {\n fields {\n key: \"image\"\n value {\n struct_value {\n fields {\n key: \"src\"\n value {\n struct_value {\n fields {\n key: \"rawUrl\"\n value {\n string_value: \"https://example.com/images/logo.png\"\n }\n }\n }\n }\n }\n }\n }\n }\n fields {\n key: \"subtitle\"\n value {\n string_value: \"Please call us on 18009428274 or 911\"\n }\n }\n fields {\n key: \"text\"\n value {\n string_value: \"<b> Stop </b> what you are doing <br> <b> Go </b> outside immediately <br> <b> Let us know </b> After making your report, a representative will come to your location and check for potential leaks or faulty appliances.\"\n }\n }\n fields {\n key: \"title\"\n value {\n string_value: \"Emergency\"\n }\n }\n fields {\n key: \"type\"\n value {\n string_value: \"accordion\"\n }\n }\n }\n }\n }\n }\n }\n }\n }\n }\n lang: \"en\"\n type {\n number_value: 4.0\n }\n }\n messages {\n lang: \"en\"\n type {\n number_value: 0.0\n }\n speech {\n string_value: \"How can I help you today?\"\n }\n }\n messages {\n payload {\n fields {\n key: \"richContent\"\n value {\n list_value {\n values {\n list_value {\n values {\n struct_value {\n fields {\n key: \"options\"\n value {\n list_value {\n values {\n struct_value {\n fields {\n key: \"text\"\n value {\n string_value: \"I am existing customer needs support\"\n }\n }\n }\n }\n values {\n struct_value {\n fields {\n key: \"text\"\n value {\n string_value: \"Just browsing\"\n }\n }\n }\n }\n values {\n struct_value {\n fields {\n key: \"text\"\n value {\n string_value: \"Ask me anything \"\n }\n }\n }\n }\n }\n }\n }\n fields {\n key: \"type\"\n value {\n string_value: \"chips\"\n }\n }\n }\n }\n }\n }\n }\n }\n }\n }\n lang: \"en\"\n type {\n number_value: 4.0\n }\n }\n }\n}\nstatus {\n code: 200\n error_type: \"success\"\n}\n`;
rawdata = rawdata.replace("Dialogflow Response : ", "");
rawdata = rawdata.replace(/\\n/g, "")
.replace(/\\'/g, "\\'")
.replace(/\\"/g, '\\"')
.replace(/\\&/g, "\\&")
.replace(/\\r/g, "\\r")
.replace(/\\t/g, "\\t")
.replace(/\\b/g, "\\b")
.replace(/\\f/g, "\\f");
console.log(rawdata);
const json = rawdata
// add { at the beginning
.replace(/^/, '{')
// add } at the end
.replace(/$/, '}')
// add missing columns
.replace(/(?<=\w+)(?=\s*\{)/g, ':')
// add missing quotes to keys
.replace(/(?<=\n\s*)(\w+)(?=\s*:)/g, '"$1"')
// add missing commas
.replace(/(?<![\{\[])(?=\s*\n+\s*)(?!\s*\n+\s*[\}\]])/g, ',');
// parse json to object
const data = JSON.parse(json);
console.log(data);
let dataStr = JSON.stringify(data);
fs.writeFileSync('response.json', dataStr);
【问题讨论】:
-
“形成完美的 JSON 格式”对您意味着什么?试过
JSON.stringify? -
是尝试过但没有用。` { "session":"dfMessenger-12848231", "query_input":"{ \"text\": { \"textInputs\": [{ \"text \": \"只是浏览\" }] }}", "timezone":"America/Barbados" }` 需要这样。
-
您能否包含您正在使用的源数据以及您尝试格式化 JSON 字符串的 Minimal, Complete, and Reproducible 示例?
-
我已经用确切的源数据更新了这个问题。我已经添加了我的源代码。请检查并帮助我
-
如果此数据是某些 API 响应的一部分,那么您也可以尝试将其作为 JSON 响应进行处理,例如 response.json()。您是否在某些服务器端处理过程中接收这些数据?
标签: javascript jquery node.js json reactjs