Tuesday, 20 September 2022

Working with array of tables in TOML

Array of tables can be expressed by using a header with a name in double brackets.

 

Signature

[[header]]
	# First Table definition
[[header]]
	# Second Table definition
[[header]]
	# Third Table definition

The first instance of the header defines the array and its first table element, and each subsequent instance creates and defines a new table element in that array. T

 

For example, following snippet document array of employees.

 

arrayOfTables.toml

[[employees]]
  firstName = "Krishna"
  lastName = "G"
  address.city= "Hyderabad"
  address.country = "India"

[[employees]]
  firstName = "Ram"
  lastName = "Majety"
    age = 34

[[employees]]
  firstName = "Ram"
  lastName = "Majety"
    age = 34
    address = {city = "Bangalore", "country" = "India"}

Above snippet is equivalent to following json document.

{
  "employees": [
    {
      "firstName": "Krishna",
      "lastName": "G",
      "address": {
        "city": "Hyderabad",
        "country": "India"
      }
    },
    {
      "firstName": "Ram",
      "lastName": "Majety",
      "age": 34
    },
    {
      "firstName": "Ram",
      "lastName": "Majety",
      "age": 34,
      "address": {
        "city": "Bangalore",
        "country": "India"
      }
    }
  ]
}

Any reference to an array of tables points to the most recently defined table element of the array. This allows you to define sub-tables, and even sub-arrays of tables, inside the most recent table. Have a look at below example to get quick glance.

 


arrayOfTables2.toml

[[employees]]
  firstName = "Krishna"
  lastName = "G"
 
 [[employees.workExperience]]
  organization = "ABC Corp"
    experience = 4.3
 [[employees.workExperience]]
  organization = "XYZ Corp"
    experience = 1.8
 [[employees.workExperience]]
  organization = "Demo Corp"
    experience = 10.8
  
 [[employees]]
  firstName = "Ram"
  lastName = "Majety"
  
 [[employees.addresses]]
 city = "Bangalore"
 country = "India"
 street = "Chowdeswari"
 isPermanentAddress = true
 
[[employees.workExperience]]
  organization = "ACD Corp"
    experience = 3.5
[[employees.workExperience]]
  organization = "DEF Corp"
    experience = 7.11

Above snippet is equivalent to following JSON document.

{
  "employees": [
    {
      "firstName": "Krishna",
      "lastName": "G",
      "workExperience": [
        {
          "organization": "ABC Corp",
          "experience": 4.3
        },
        {
          "organization": "XYZ Corp",
          "experience": 1.8
        },
        {
          "organization": "Demo Corp",
          "experience": 10.8
        }
      ]
    },
    {
      "firstName": "Ram",
      "lastName": "Majety",
      "addresses": [
        {
          "city": "Bangalore",
          "country": "India",
          "street": "Chowdeswari",
          "isPermanentAddress": true
        }
      ],
      "workExperience": [
        {
          "organization": "ACD Corp",
          "experience": 3.5
        },
        {
          "organization": "DEF Corp",
          "experience": 7.11
        }
      ]
    }
  ]
}




 

Previous                                                 Next                                                 Home

No comments:

Post a Comment