1 回答
TA贡献1828条经验 获得超3个赞
这是一种使用正则表达式尝试修复它的解决方案,不确定它是否适用于所有情况,但它适用于这种情况:
const raw = `
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.0
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 {
platform: "google"
display_text: "Hi,I'm Sathvish How can I help you?"
lang: "en"
type {
string_value: "simple_response"
}
}
messages {
platform: "telephony"
lang: "en"
type {
number_value: 14.0
}
text: "Hi, I’m Vishnu from The MDC. For all emergencies, Please call 9-1-1 immediately! Or call us on 123456789. So, are you existing customer?"
}
messages {
lang: "en"
type {
number_value: 0.0
}
speech {
string_value: "Hi, I'm sathvish How can I help you"
}
}
messages {
payload {
fields {
key: "richContent"
value {
list_value {
values {
list_value {
values {
struct_value {
fields {
key: "image"
value {
struct_value {
fields {
key: "src"
value {
struct_value {
fields {
key: "rawUrl"
value {
string_value: "https://google.com/images/logo.png"
}
}
}
}
}
}
}
}
fields {
key: "subtitle"
value {
string_value: "Please call us 123456"
}
}
fields {
key: "text"
value {
string_value: "<b> Stop </b> what you are doing <br> <b> Go </b> outside immediately."
}
}
fields {
key: "title"
value {
string_value: "Emergency"
}
}
fields {
key: "type"
value {
string_value: "accordion"
}
}
}
}
}
}
}
}
}
}
lang: "en"
type {
number_value: 4.0
}
}
messages {
lang: "en"
type {
number_value: 0.0
}
speech {
string_value: "How can I help you today?"
}
}
messages {
payload {
fields {
key: "richContent"
value {
list_value {
values {
list_value {
values {
struct_value {
fields {
key: "options"
value {
list_value {
values {
struct_value {
fields {
key: "text"
value {
string_value: "I am existing customer needs support"
}
}
}
}
values {
struct_value {
fields {
key: "text"
value {
string_value: "Just browsing"
}
}
}
}
values {
struct_value {
fields {
key: "text"
value {
string_value: "Ask me anything "
}
}
}
}
}
}
}
fields {
key: "type"
value {
string_value: "chips"
}
}
}
}
}
}
}
}
}
}
lang: "en"
type {
number_value: 4.0
}
}
}
}
status {
code: 200
error_type: "success"
}
`;
const json = raw
// 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);
添加回答
举报