How to deserialize custom payload response in Dialogflow v2?
this is built using Struct protocols
In order to convert that you would need to use google proto buffer i.e google.protobuf I used the below code to do this.
import google.protobuf as pf
pf.json_format.MessageToJson(response.query_result.fulfillment_messages[1].payload, including_default_value_fields=False)
Edit: Apparently Dialogflow released their own solution with the samples for the Node.js SDK: dialogflow-nodejs-client-v2/samples/structjson.js
Alternatively to the solution of Vikas Jain you could write your own (recursive) conversion function.
Something like:
processCustomPayloadMessage(object) {
let outputMessage = Array.isArray(object) ? [] : {};
Object.entries(object).forEach(([key, value]) => {
if (value.kind == 'structValue') {
outputMessage[key] = this.processCustomPayloadMessage(value.structValue.fields);
} else if (value.kind == 'listValue') {
outputMessage[key] = this.processCustomPayloadMessage(value.listValue.values);
} else if (value.kind == 'stringValue') {
outputMessage[key] = value.stringValue;
} else if (value.kind == 'boolValue') {
outputMessage[key] = value.boolValue;
} else {
outputMessage[key] = value;
}
});
return outputMessage;
}
// call this method with your response message
this.processCustomPayloadMessage(message.payload.fields);