1

With this code:

var button = Value.ForStruct(new Struct{
    Fields={
        ["type"] = Value.ForString("postback"),
        ["title"] = Value.ForString("Call Representative"),
        ["payload"] =  Value.ForString("+15105551234"),
    }
});

var inPayload = Value.ForStruct(new Struct{
    Fields ={
        ["buttons"] = Value.ForList(button),
        ["text"] = Value.ForString("try the postback"),
        ["template_type"] = Value.ForString("button"),
    }
});

var attachment = Value.ForStruct(new Struct{
    Fields ={
        ["payload"] = inPayload,
        ["type"] = Value.ForString("template"),
    }
});

var msg = Value.ForStruct(new Struct{
    Fields ={
        ["attachment"] = attachment,
    });

Payload = new Struct{
    Fields ={
        ["facebook"] = msg
    }

I was able to create the following json:

"payload":    {
      "facebook": {"attachment":       {
         "payload":          {
            "buttons": [            {
               "type": "postback",
               "title": "Call Representative",
               "payload": "+15105551234"
            }],
            "text": "try the postback",
            "template_type": "button"
         },
         "type": "template"
      }}

Now I need to create the following other format but I dont find how to do it:

"payload":    {
         "message": "Yes I did it"
         "platform": "kommunicate",
         "attachment":       {
             "payload":          {
                "buttons": [            {
                   "type": "postback",
                   "title": "Call Representative",
                   "payload": "+15105551234"
                }],
                "text": "try the postback",
                "template_type": "button"
             },
             "type": "template"
          }

I really dont find how to eliminate the first "facebook": { element and leave only:

  {
    "message": "Yes I did it",
    "platform": "kommunicate",
    "attachment":

And include message and platform at the same level. Here is the complete json I will like to generate:

"payload":    {
   "platform": "kommunicate",
   "message": "Yes I did it",
   "attachment":       {
         "payload":          {
            "buttons": [            {
               "type": "postback",
               "title": "Call Representative",
               "payload": "+15105551234"
            }],
            "text": "try the postbackggggggg",
            "template_type": "button"
         },
         "type": "template"
      }
phuzi
  • 12,078
  • 3
  • 26
  • 50
Ray
  • 483
  • 4
  • 17
  • your title mentions protobuf, but nothing in the description mentions this. Json and protobuf are both formats for serializing data, but they do it in a very different way. – JonasH Feb 21 '20 at 14:53
  • You really should look into using classes to create a JSON representation along with Newtonsoft.Json – Jawad Feb 21 '20 at 14:58
  • @user104903 the Google protobuf API includes both binary protobuf and JSON handling. – Marc Gravell Feb 21 '20 at 15:04
  • I'm sorry you are right, The question was really about Google Dialogflow API v2 but I was working with protobuf at the moment and for some reason I use this as the title. I'm really sorry but I dont know where I can edit the question. – Ray Feb 21 '20 at 15:56

1 Answers1

2

If you want to take an object and convert it to json I would recommend taking a look at Newtonsoft Json.Net library. They have plenty of examples that might help you. There is also protobuf.net library for serializing to protobuf instead of json.

Both libraries are used in similar ways, you create a class with appropriate properties and set the values you want. You will need multiple classes for nested types as in your example. Protobuf requires you to annotate the properties with attributes, while this is optional for json.net. You then send the object to the serialization library and get a string or binary data representing your object. This kind of object is often called a Data Transfer Object (DTO), since the only purpose it has is to aid in serialization or/and transfering the data to another system.

JonasH
  • 28,608
  • 2
  • 10
  • 23
  • Do you have any idea if the resulting JSON from newtonsoft can be pass as a Value to DialogFlow API v2. This part in particular: Payload = new Struct{ Fields ={ ["facebook"] = msg } – Ray Feb 21 '20 at 15:53
  • @ray yes, Json.Net should produce files that should work with anything that consumes consumes json. You might need to keep in mind what text encoding is used. – JonasH Feb 24 '20 at 15:38