24

I am trying to read parameters from a YAML file into Javascript. Is there any good library to do this?

I've tried these libraries: https://github.com/nodeca/js-yaml and http://code.google.com/p/javascript-yaml-parser/

but both libraries only have functions that parse YAML when it is given as a string, instead of parsing straight out of a .yml or .yaml file. Are there any parsers that read YAML from a file and convert them to JS objects?

nsax91
  • 437
  • 2
  • 10
  • 18
  • There are multiple examples on how to parse `*.yml` files in the first library you linked **js-yaml** – jaime Dec 09 '12 at 07:11

3 Answers3

20

It seemed to be hard to find a good example of using js-yaml from a browser. Probably because they emphasize the use of the parser in node.js.

The parser is at https://github.com/nodeca/js-yaml. Use NPM to install it

npm install js-yaml

and grab the js-yaml.js file from the node_modules/js-yaml/bin directory.

Here is a quick, simple demo that loads a YAML file, uses js-yaml to parse it into objects, and then (for verification) it does a native JSON.stringify to convert the JSON into a string and finally uses jquery $.parseJSON to verify the resulting JSON.

(function () {
"use strict";
    $(document).ready(function () {
        $.get('/common/resources/LessonContentsLv01Ln01.yml')
        .done(function (data) {
          console.log('File load complete');
          console.log(jsyaml.load(data));
          var jsonString = JSON.stringify(data);
          console.log(jsonString);
          console.log($.parseJSON(jsonString));
      });
    });
}());

And the HTML

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Read YAML</title>
                 <script src="//code.jquery.com/jquery-2.1.0.js">
        </script><script src='/vendor/js/js-yaml.js'>
        </script><script src='/test/readYaml.js'>
    </script>
</head>
<body>
</body>
</html>

Github repository https://github.com/nodeca/js-yaml

wolfstevent
  • 703
  • 8
  • 13
15

js-yaml does. I found this by Googling "node js yaml" because reading from files in JavaScript is done server side with node.js (or something like it), not from a browser.

The README for js-yaml begins

usage: js-yaml [-h] [-v] [-c] [-j] [-t] file

Positional arguments:

file File with YAML document(s)

That is pretty strong evidence that it does process YAML directly from files.

hsz
  • 148,279
  • 62
  • 259
  • 315
Ray Toal
  • 86,166
  • 18
  • 182
  • 232
  • js-yaml used a "require" call though. I have little to no experience with node.js or anything server-side, and for this project I'm just looking for a client-side parser that will run in the browser. – nsax91 Dec 12 '12 at 06:40
  • 1
    I don't know of any client-side solutions for reading yaml files. I think the best you can do is combine file reading techniques in client-side JavaScript (see [http://stackoverflow.com/questions/4950567/reading-client-side-text-file-using-javascript](this question) for some of these techniques) then combine them with the yaml parsing of strings. – Ray Toal Dec 12 '12 at 06:48
  • js-yaml is really frustating if you want to keep floating points value like 0.0 or 25.0 as is. When you load your yaml they will become ints so 0 and 25 which is really annoying – Greg7000 Jul 12 '23 at 17:18
3

If you want to parse it in web browser, you can load the yaml file you want to parse in script tag and read it's content using js code, which will provide you string result. And if you want to parse yaml in nodejs environment, you can read file directly and also got string. I don't think this is a problem from directly parse yaml or parse yaml from string.

cattail
  • 337
  • 1
  • 3
  • 9
  • It depends of the complexity of the yaml file. If it is only raw values it is ok. If it has references or anchor, a proper parser will be safer. – Lyokolux Feb 08 '23 at 10:18