JSON and its data types

JSON is a data record in the form of a string that is understandable to both a machine and a person. JSON is a fairly strict format, but at the same time it is very simple.

Any of the possible data types can be written to JSON. If you understand how different data types look like in JSON, it will be easier to write it yourself, for example, in the request body to another service or to shorten the entry of a variable in the bot.

If you can read JSON, it will be easy to understand the Users section and the variables in it.

The builder relies on JSON in its work with variables, so understanding JSON is an important part of understanding variables in Botmother.

Simple Data Types

The strings are written in direct double "programmer" quotes: "Hello world".

The numbers are written simply, without quotes or anything else: 1.

Logical values, as well as numbers, are written without quotes or anything else: true or false.

NULL, i.e. nothing, is written without quotes in lowercase: null.

Composite Data Types

Composite types — lists and objects — are called “composite” because they are filled with values of other types inside, including other nested objects and lists.

Object

To write an object, open the curly brace {, then specify the key (the name of the variable nested in the object) as a string, i.e. it should be written necessarily in direct double quotes, then put a colon, and write the value in any available data type.

If you need to add another value, put a comma and repeat the entry "key": value. When you enumerate all the keys and values have, the object is closed with a curly brace }. Spaces and string breaks are not taken into account, they can be placed at your discretion.

Let's try to write an example and take an object from the section about Objects and dictionaries:

{
   "name": "James",
   "role": "admin",
   "age": 18,
   "gender": "male",
   "isRoot": true
}

This is what an object written in JSON looks like.

  • name, role, gender are strings, they are in double quotes.
  • age is a number, it is written without quotes and other things.
  • isRoot is a logical variable with the true value.
A comma is not put after the last pair of "key": the value.

You can learn more about objects and dictionaries.

List

Lists are written in square brackets. The list items are separated by commas.

Do not put comma after the last item in the list.

The list of names that we compiled earlier is written in JSON like this:

["John", "Emily", "Sophia", "Daniel"]

It is not necessary that all the elements of the list are of the same type, the list may consist of different data types:

["John", 123, false, null]

Such a list is considered acceptable, but do not abuse it — it can cause errors in the logic of your bot. You may forget or not take into account that different data types are stored in the same list.

Nested object

Objects can be nested into each other. Anything can be the value in an object, even another object. Let's supplement our user object with another object with his place of residence:

{
  "name": "James",
  "role": "admin",
  "age": 18,
  "home": {
    "country": "UK",
    "city": "Hogsmith",
    "street": "Yew Street",
    "building": "10",
    "zip": 100115
  }
}

List in the object

Lists can also be nested in objects:

{
  "name": "James",
  "age": 18,
  "children": ["Sarah", "Sam"]
}

Objects in lists

This is also allowed:

{
  "name": "James",
  "age": 18,
  "children": [
    { "name": "Sarah", "age": 3 },
    { "name": "Sam", "age": 5 }
  ]
}

List in the list

And it is also possible:

{
  "type": "rect",
  "coords": [[0, 0], [10, 10]]
}

Empty object

An object may not contain any nested variables at all. Such an object is called empty. It is written simply by opening and immediately closing curly brackets: {}.

Empty list

The list may not contain any data at all. Such a list is called empty. It is written simply by opening and immediately closing square brackets: [].

Valid and invalid JSON

A valid JSON is a JSON in which the rules for recording all its components are not violated.

All strings are enclosed in double quotes. Object keys are strings. In objects and lists, there is no comma after the last element, numbers are written without spaces, and data types that are invalid in JSON are not used. All objects are correctly opened {and closed }, i.e. the balance of parentheses is observed. The same applies to lists.

There is always an object or a list at the "top level", though, it is an object more often.

Just writing any other valid JSON value is not prohibited, you just won't be able to write other variables.

For example, "John" is a valid JSON that consists of just one line.

There can be any other valid JSON inside the values in the objects and inside the list items. But only strings can be the keys of an object.

Numbers can be object keys only if they are written as strings, i.e. in double quotes. Actually, they will be strings:

{
  "1": "Bonnie",
  "2": "Clyde"
}

If you didn’t get enough information, you can read the article on Wikipedia.

JSON and variables in the Botmother

User variables can be viewed in the Users section. If you open the card of an individual user, all variables recorded by you or created automatically can be viewed in JSON format. The knowledge gained in this article can help you figure this out.

Accessing a variable from a nested object

If there is an object with another object inside in the user variables, and you need to get variables from there, you can do it using a dot after the name of the object from which we get the variable.

For example, we have a user object in user variables and we want to send the country of his home address in a message.

{
  "user": {
    "name": "James",
    "age": 18,
    "children": [
      { "name": "Sarah", "age": 3 },
      { "name": "Sam", "age": 5 }
    ],
    "home": {
      "country": "UK",
      "city": "Hogsmith",
      "street": "Yew Street",
      "building": "10",
      "zip": 100115
    }
  }
}

We can do this using the template: {{user.home.country}}. Each time we move to a more nested object, we put a dot until we get the name of the nested variable we need.

This is also possible with lists, only the index of the desired item from the list must be specified in square brackets. For example, to get the name of the first child, we can use the following template: {{user.children.[0].name}}. Pay attention to 0 — the elements of the lists are numbered from zero, i.e. the first element of the list is at index 0.

Turning a variable into JSON

You can turn almost any variable in Botmother into JSON. To do this, you can use the stringify helper.

This helper is convenient for forming a body in the Request component or simply logging variables into the chat while debugging the bot.

Using the helper to generate JSON is very simple: {{stringify user}}.

To the beginning ↑