wegry wegry - 28 days ago 6
YAML Question

YAML equivalent of array of objects in JSON

I have a JSON array of objects that I'm trying to convert to YAML.

{"AAPL": [
{
"shares": -75.088,
"date": 11/27/2015
},
{
"shares": 75.088,
"date": 11/26/2015
},
]}


Is there an equivalent representation in YAML that isn't just JSON? I'd like to do something like

AAPL:
- :
shares: -75.088
date: 11/27/2015
- :
shares: 75.088
date: 11/26/2015


but the cleanest thing I've come up with is

AAPL:
- {
shares: -75.088,
date: 11/27/2015
}
{
shares: 75.088,
date: 11/26/2015
}

Answer Source

You just want this:

AAPL:
  - shares: -75.088
    date: 11/27/2015
  - shares: 75.088
    date: 11/26/2015

This is the equivalent JSON:

{
  "AAPL": [
    {
      "date": "11/27/2015", 
      "shares": -75.087999999999994
    }, 
    {
      "date": "11/26/2015", 
      "shares": 75.087999999999994
    }
  ]
}

You can see it (and edit it interactively) here.