Tuesday 20 September 2022

Working with tables in TOML

Table is similar to Hash table, dictionary, Map in programming languages. Tables are defined with headers.

 

Signature

[table_header]
key1 = val1
key2 = val2

tables1.toml

[employee]
firstName = "Lakshmana Rao"
lastName = "Gurram"
age = 34

[employee.address]
city = "Bangalore"
street = "Chowdeswari"
country = "Bangaore"

Above snippet is equivalent to following json document.

{
  "employee": {
    "firstName": "Lakshmana Rao",
    "lastName": "Gurram",
    "age": 34,
    "address": {
      "city": "Bangalore",
      "street": "Chowdeswari",
      "country": "Bangaore"
    }
  }
}

By any chance if the key in a table has dot (.) in it, enclose the key in double quotes while defining.

 


tables2.toml

[complexTable]
a = 10

[complexTable."point.coordinates"]
x = 10
y = 20

[complexTable."3dpoint.coordinates"]
demo1.x = 10
demo1.y = 20
demo1.z = 30
demo2.x = 11
demo2.y = 12
demo2.z = 13

Above snippet is equivalent to following json.

{
  "complexTable": {
    "a": 10,
    "point.coordinates": {
      "x": 10,
      "y": 20
    },
    "3dpoint.coordinates": {
      "demo1": {
        "x": 10,
        "y": 20,
        "z": 30
      },
      "demo2": {
        "x": 11,
        "y": 12,
        "z": 13
      }
    }
  }
}

Points to remember

a.   Empty tables are allowed

b.   You can’t define a table more than once.



Previous                                                 Next                                                 Home

No comments:

Post a Comment