16

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?

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
R891
  • 2,550
  • 4
  • 18
  • 30

2 Answers2

19

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" }
  ]
}
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Paul Razvan Berg
  • 16,949
  • 9
  • 76
  • 114
11

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"
    }]
}
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
R891
  • 2,550
  • 4
  • 18
  • 30