Rob Bos
Rob Bos

Reputation: 1471

npm parse yaml to object with array instead of named objects

I'm using npm parse yaml to parse yaml to an object and I get a group of named objects instead of an array. This is caused by the objects not having been prefixed with a dash (I think).

How can I convert the group of named objects to an array, or tell the parsers that this is what I want?

Yaml (partial):

jobs:
  job-a:
    property-1: value

  job-b:
    property-1: different value

Using const parsed = YAML.parse(content) gives me this object (JSON representation my own):

 object.jobs = {
   "job-a": { "property-1": "value"},
   "job-b": { "property-1": "different value"} 
 }

So the jobs object contains properties "job-a" and "job-b" instead of an array that I can iterate on.

I've been looking at the docs, but that seems more focused on the setup of creating the YAML and I cannot figure out how to get this done.

Upvotes: 2

Views: 1349

Answers (1)

Ellesmera
Ellesmera

Reputation: 11

Looks like you need to check YAML syntax. YAML example you've presented contains exactly the object "jobs" with properties "job-a" and "job-b". If you need an array, you should modify your YAML by adding list of jobs:

jobs:
  - job-a:
      property-1: value
  - job-b:
      property-1: different value

So, YAML.parse(content) will give you array inside jobs:

{
  "jobs": [
    {
      "job-a": {
        "property-1": "value"
      }
    },
    {
      "job-b": {
        "property-1": "different value"
      }
    }
  ]
}

Upvotes: 1

Related Questions