Does TOML support nested arrays of objects/tables?
Asked Answered
S

3

23

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?

Snot answered 26/2, 2018 at 22:18 Comment(0)
T
26

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" }
  ]
}
Tisiphone answered 7/3, 2020 at 13:39 Comment(0)
S
16

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"
    }]
}
Snot answered 26/2, 2018 at 23:5 Comment(0)
B
0

It's worth noting that TOML also supports inline-tables, while they can't be split across multiple lines - values within them can.

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.
another line"""}
]

Converts into JSON:

{
    "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"
        }
    ]
}
Burg answered 26/7 at 6:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.