Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested object in TOML

Tags:

toml

I have the following JSON:

{
  "profile": {
    "ci": {
      "fuzz": {
        "runs": 1000
      }
    }
  }
}

Which I know I can write in TOML like this:

[profile.ci.fuzz]
runs = 1000

The problem is that I have multiple profiles, and writing profile.NAME.fuzz for all of them is rather repetitive.

I would like to ideally write the TOML like this:

[profile.ci]
fuzz = {
    runs = 1000
}

However, that didn't work. I got this syntax error:

expected a table key, found a newline at line 2 column 9

How can I define nested objects in TOML?

like image 901
Paul Razvan Berg Avatar asked Oct 31 '25 06:10

Paul Razvan Berg


1 Answers

TOML calls inline tables the objects defined within curly braces. Newlines are allowed for strings and arrays, but not for inline tables, from the specs:

Inline tables are intended to appear on a single line. A terminating comma (also called trailing comma) is not permitted after the last key/value pair in an inline table. No newlines are allowed between the curly braces unless they are valid within a value. Even so, it is strongly discouraged to break an inline table onto multiples lines. If you find yourself gripped with this desire, it means you should be using standard tables.

Regarding your example, this works:

[profile.ci]
fuzz = { runs = 1000 }

Something like this would also be allowed:

profiles = [
    { name = "foo", runs = 100 },
    { name = "bar", runs = 200 }
]
like image 112
Michele Federici Avatar answered Nov 03 '25 07:11

Michele Federici



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!