G
G
grafist025112018-06-17 11:11:32
Python
grafist02511, 2018-06-17 11:11:32

How to pass a vk api keyboard JSON object as a string?

Keyboards for chatbots have become available on VKontakte. The documentation says that "Keyboard JSON object is passed as a string"
Here is an example of a keyboard in JSON

// клавиатура вида 
// | Red | Green | 
// | White | Blue | 
{ 
// ...другие поля объекта Message 
  keyboard: { 
    "one_time": false, 
    "buttons": [ 
      [{ 
        "action": { 
          "type": "text", 
          "payload": "{\"button\": \"1\"}", 
          "label": "Red" 
        }, 
        "color": "negative" 
      }, 
     { 
        "action": { 
          "type": "text", 
          "payload": "{\"button\": \"2\"}", 
          "label": "Green" 
        }, 
        "color": "positive" 
      }], 
      [{ 
        "action": { 
          "type": "text", 
          "payload": "{\"button\": \"3\"}", 
          "label": "White" 
        }, 
        "color": "default" 
      }, 
     { 
        "action": { 
          "type": "text", 
          "payload": "{\"button\": \"4\"}", 
          "label": "Blue" 
        }, 
        "color": "primary" 
      }] 
    ] 
  } 
}

How can I turn this json into a string in Python?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladislav Kadun, 2018-06-17
@grafist02511

import json

data = {'jsonKey': None}

json.dumps(data)
>>> '{"jsonKey": null}'

json.loads(json.dumps(data))
>>> {u'jsonKey': None}

Source: https://stackoverflow.com/questions/34600003/conve...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question