YAML equivalent of array of objects in JSON
Asked Answered
G

3

352

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
  }
Grapefruit answered 29/11, 2015 at 23:59 Comment(2)
You can use online converter, like this: convertsimple.com/convert-javascript-object-to-yamlWriggly
we can check this tutorials as well: - w3schools.io/file/yaml-array-objects/….Virchow
M
686

TL;DR

You want this:

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

Mappings

The YAML equivalent of a JSON object is a mapping, which looks like these:

# flow style
{ foo: 1, bar: 2 }
# block style
foo: 1
bar: 2

Note that the first characters of the keys in a block mapping must be in the same column. To demonstrate:

# OK
   foo: 1
   bar: 2
# Parse error
   foo: 1
    bar: 2

Sequences

The equivalent of a JSON array in YAML is a sequence, which looks like either of these (which are equivalent):

# flow style
[ foo bar, baz ]
# block style
- foo bar
- baz

In a block sequence the -s must be in the same column.

JSON to YAML

Let's turn your JSON into YAML. Here's your JSON:

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

As a point of trivia, YAML is a superset of JSON, so the above is already valid YAML—but let's actually use YAML's features to make this prettier.

Starting from the inside out, we have objects that look like this:

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

The equivalent YAML mapping is:

shares: -75.088
date: 11/27/2015

We have two of these in an array (sequence):

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

Note how the -s line up and the first characters of the mapping keys line up.

Finally, this sequence is itself a value in a mapping with the key AAPL:

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

Parsing this and converting it back to JSON yields the expected result:

console.log(jsyaml.load(`
AAPL:
  - shares: -75.088
    date: 11/27/2015
  - shares: 75.088
    date: 11/26/2015
`));
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-yaml/4.1.0/js-yaml.min.js"></script>

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

Mansoor answered 30/11, 2015 at 0:10 Comment(4)
I've updated my question to reflect that there are multiple items in the list containing shares and dates pairs.Grapefruit
@wegry: Doesn't make a difference. See also the example from the YAML website: yaml.org/start.html .Saltation
"YAML is a superset of JSON, so the above is already valid YAML" So all JSON is valid YAML basically.Mccurdy
For anyone reading this, please also take a look at ChalkTalk's answer which offers a much more readable and maintainable format by putting the - array markers on their own lines. Both ways of formatting it represent the same data structure. In fact, Jordan, I might suggest that you edit your own answer to use that improved format.Tarnetgaronne
C
26

To supplement the accepted answer if the tight spacing bothers you, you can also do:

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

...this is adapted straight from the YAML specification's example 2.4 "Sequence of Mappings".

Constringent answered 5/6, 2021 at 0:45 Comment(2)
awesome just what i needed. Because I prefer 4 space indent instead of 2.Pandean
Thank you for posting this. I just discovered it myself a few minutes ago, and I like it much better than the other answer. It is more readable and easier to maintain.Tarnetgaronne
Q
20

Great answer above. Another way is to use the great yaml jq wrapper tool, yq at https://github.com/kislyuk/yq

Save your JSON example to a file, say ex.json and then

yq -y '.' ex.json

AAPL:
- shares: -75.088
  date: 11/27/2015
- shares: 75.088
  date: 11/26/2015
Queenqueena answered 24/10, 2018 at 19:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.