This tutorial explains how to represent collection iterable types in TOML format.
- Array list
- Array of Arrays
- Array of Objects
- String Array
TOML Array list
An array is a group of similar values with a single name.
In TOML, an Array represents a single key mapped to multiple values.
All values are enclosed in square brackets []
.
Array elements can be of different data types.
Here is the syntax for toml arrays
.
# Example of an array of string types in TOML
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, enclosed in double square brackets [[]]
.
[[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
{
"employees": [
{
"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.
Key is placed with [[]]
An array of objects contains an object list.
Here is a code for the TOML array of objects example
[[one]]
id = 1
name = "franc"
[[one]]
id = 11
name = "Tom"
Here the same key exists for multiple objects. 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.
# Example of an array of string types in TOML
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.
# Example of an array of numbers 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
# Example of an array of floating numbers types in TOML
numbers1 = [ 1.0, 2.0, 3.0, 4.1 ]
JSON is
{
"numbers1": [
1.0,
2.0,
3.0,
4.1
]
Arrays of Mixed Types in TOML
The array contains mixed types such as strings, numbers, and floating numbers.
# Example of an array of mixed types in TOML
numbers = ["one", "two", 3.0, 4 ]
TOML 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 & Objectsin TOML
You can create an empty array in TOML files using empty square brackets.
# Declare an empty array
students = []
Elements are added to an empty array as given below
# Adding items to the array
students = ["johny", "tony", "eric"]
Let’s see an Empty object create
# Declare an empty object
students = {}
adds elements to an object
# Adding key-value pairs to the object
students.id = 11
students.name = "Eric"