Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does TOML support nested arrays of objects/tables?

Tags:

json

toml

I want to generate JSON from TOML files. The JSON structure should be something like this, with arrays of objects within arrays of objects:

{
    "things": [
        {
            "a": "thing1",
            "b": "fdsa",
            "multiline": "Some sample text."
        },
        {
            "a": "Something else",
            "b": "zxcv",
            "multiline": "Multiline string",
            "objs": [  // LOOK HERE
                { "x": 1},
                { "x": 4 },
                { "x": 3 }
            ]
        },
        {
            "a": "3",
            "b": "asdf",
            "multiline": "thing 3.\nanother line"
        }
    ]
}

I have some TOML that looks like the example below, but it doesn't seem to work with the objs section.

name = "A Test of the TOML Parser"

[[things]]
a = "thing1"
b = "fdsa"
multiLine = """
Some sample text."""

[[things]]
a = "Something else"
b = "zxcv"
multiLine = """
Multiline string"""
[[things.objs]]  # MY QUESTION IS ABOUT THIS PART
x = 1
[[things.objs]]
x = 4
[[things.objs]]
x = 3

[[things]]
a = "3"
b = "asdf"
multiLine = """
thing 3.
another line"""

Is there a way to do it in TOML? JSON to TOML converters don't seem to work with my example. And does it work with deeper nesting of arrays of arrays/tables?

like image 220
R891 Avatar asked Feb 26 '18 22:02

R891


People also ask

What is a TOML table?

TOML aims to be a minimal configuration file format that's easy to read due to obvious semantics. TOML is designed to map unambiguously to a hash table. TOML should be easy to parse into data structures in a wide variety of languages.

Is TOML whitespace sensitive?

TOML is case sensitive. Whitespace means tab (0x09) or space (0x20).

What is TOML used for?

TOML is a file format for configuration files. It is intended to be easy to read and write due to obvious semantics which aim to be "minimal", and is designed to map unambiguously to a dictionary.


2 Answers

As per the PR that merged this feature in the main TOML repository, this is the correct syntax for arrays of objects:

[[products]]
name = "Hammer"
sku = 738594937

[[products]]

[[products]]
name = "Nail"
sku = 284758393
color = "gray"

Which would produce the following equivalent JSON:

{
  "products": [
    { "name": "Hammer", "sku": 738594937 },
    { },
    { "name": "Nail", "sku": 284758393, "color": "gray" }
  ]
}
like image 152
Paul Razvan Berg Avatar answered Oct 24 '22 04:10

Paul Razvan Berg


I'm not sure why it wasn't working before, but this seems to work:

name = "A Test of the TOML Parser"

[[things]]
a = "thing1"
b = "fdsa"
multiLine = """
Some sample text."""

[[things]]
a = "Something else"
b = "zxcv"
multiLine = """
Multiline string"""
[[things.objs]]
x = 1
[[things.objs]]
x = 4
[[things.objs]]
x = 7
[[things.objs.morethings]]
y = [
    2,
    3,
    4
]
[[things.objs.morethings]]
y = 9

[[things]]
a = "3"
b = "asdf"
multiLine = """
thing 3.
another line"""

JSON output:

{
    "name": "A Test of the TOML Parser",
    "things": [{
        "a": "thing1",
        "b": "fdsa",
        "multiLine": "Some sample text."
    }, {
        "a": "Something else",
        "b": "zxcv",
        "multiLine": "Multiline string",
        "objs": [{
            "x": 1
        }, {
            "x": 4
        }, {
            "x": 7,
            "morethings": [{
                "y": [2, 3, 4]
            }, {
                "y": 9
            }]
        }]
    }, {
        "a": "3",
        "b": "asdf",
        "multiLine": "thing 3.\\nanother line"
    }]
}
like image 41
R891 Avatar answered Oct 24 '22 04:10

R891