TOML Array list
An array is a group of similar values with a single name.
In TOML, Array represents a single key mapped to multiple values.
All values are enclosed in square brackets []
.
Here is the syntax for toml arrays
.
key1 = [ "value1", "value2", "value3", "value4", "value5" ]
The json representation is as below.
{
"key1": [
"value1",
"value2",
"value3",
"value4",
"value5"
]
}
Toml values do not allow without a key and throw an error Below is not a valid toml configuration item.
[one, two, three, four]
TOML Arrays of Arrays
Arrays of Arrays are also called multi-dimensional arrays or nested arrays.
use indentation syntax
with each parent element as the section.
[[emloyees]]
id = 213
name = "franc"
[[emloyees.others]]
department = "sales"
did = 1
[[emloyees.others]]
salary = 5_000
[[emloyees.others]]
address = "USA"
pincode = 97_845
equivalent JSON is
{
"emloyees": [
{
"id": 213,
"name": "franc",
"others": [
{
"department": "sales",
"did": 1
},
{
"salary": 5000
},
{
"address": "USA",
"pincode": 97845
}
]
}
]
}
TOML Arrays of Objects
Objects contain multiple key and value pairs. An array of objects contains an object list.
Here is a code for TOML array of objects example
[[one]]
id = 1
name = "franc"
[[one]]
id = 11
name = "Tom"
The same above can be TOML equivalent of the array of objects example as follows
{
"one": [
{
"id": 1,
"name": "franc"
},
{
"id": 11,
"name": "Tom"
}
]
}
Arrays of String in TOML
Strings contain a group of characters. It is an Inbuilt data type in TOML.
Keys in the string array are optional in TOML. Here is an example of an array of strings with keys.
numbers = [ "one", "two", "three", "four" ]
Here is an equivalent JSOn representation
{
"numbers": [
"one",
"two",
"three",
"four"
]
}
Arrays of numbers in TOML
Numbers are predefined scalar types in TOML. It accepts either integer or floating numbers only.
It does not allow mixed types in toml
numbers = [ 1, 2, 3, 4 ]
JSON representation is
{
"numbers1": [
1,
2,
3,
4
]
}
Following is an example of An array of Numbers with floating values
numbers1 = [ 1.0, 2.0, 3.0, 4.1 ]
JSON is
{
"numbers1": [
1.0,
2.0,
3.0,
4.1
]
dictionaries types
Dictionaries contain keys and values. Each parent is defined as a section enclosed with square brackets[]
The following is an example of dictionary types of data.
[mysqldatabase]
hostname = "dev"
port = 3_02
username = "user"
password = "pass"
Equivalent JSON is
{
"mysqldatabase": {
"hostname": "dev",
"password": "pass",
"port": 302,
"username": "user"
}
}
How do you create an empty array in TOML
You can create an empty array in TOML files.
It throws an error for empty arrays in toml