AlexUA
AlexUA

Reputation: 747

Parsing multi-document YAML doesn't give any successful result

I have a long multi-document YAML that needs to be parsed, let me show you some fragments of what it looks like:

---
message: Object has changed  to concrete class.
date: 2022-11-30T07:33:07.992955008Z
reponame: myRepo
---
message: Type  has been created.
date: 2022-11-30T07:33:07.992956372Z
reponame: myRepo
---
message: Constant has been created.
date: 2022-11-30T07:33:07.992956372Z
reponame: myRepo

And this one needs to be converted to the array of JS-objects / JSON. I have tried several libraries, but only was supporting multi-documents sources to be parsed ( library calls yaml ), but the problem is - it's parsing function returning an array of Document type of objects (attached picture is an example what I am getting as a result, sorry I can't paste the code - sandbox is not giving such opportunity), which is not good for me.

Is there any way to turn this yaml into array of objects?

enter image description here

Upvotes: 0

Views: 686

Answers (1)

AlexUA
AlexUA

Reputation: 747

Have found the proper way. This way you can get the resulted array as a JSON:

const yaml = require("yaml");

const data = yaml.parseAllDocuments("./path/to/yamlFile");
const res = data.map((item)=>item.toJS());

Upvotes: 1

Related Questions