12
$\begingroup$

Has anyone implemented the import of YAML data files into Mathematica? I suppose some sort of transformation into a nested list of rules would be the way to do it. Thanks

$\endgroup$
1
  • $\begingroup$ There are many YAML parsers for Java, and Mathematica has J/Link. It should be possible to use those two together. $\endgroup$ Sep 30, 2014 at 12:46

3 Answers 3

6
$\begingroup$

I wrote such a cross-platform tool to convert other formats to wolfram format: WolframExchange

Usage

wex in.yml      # check file but no output
wex in.yml -t   # output only text
wex in.yml -c   # output only binary
wex in.yml -ct  # output both text and binary

Result

enter image description here

enter image description here

$\endgroup$
1
  • 2
    $\begingroup$ This is very cool! $\endgroup$
    – b3m2a1
    Mar 23, 2020 at 8:30
5
$\begingroup$

I just wrote an importer: https://github.com/zbjornson/MYaml. It's built on SnakeYAML and supports all language-independent types.

Example 2.10 from the spec:

In[1]:= ImportString["---
 hr:
   - Mark McGwire
   # Following node labeled SS
   - &SS Sammy Sosa
 rbi:
   - *SS # Subsequent occurrence
   - Ken Griffey", "YAML"]
Out[1]= {
          "hr" -> {"Mark McGwire", "Sammy Sosa"}, 
          "rbi" -> {"Sammy Sosa", "Ken Griffey"}
        }

More examples in the test suite.

$\endgroup$
9
  • 2
    $\begingroup$ For demonstration purposes, can you show how your importer acts on a sample YAML file? $\endgroup$ Oct 12, 2016 at 3:21
  • $\begingroup$ @J.M. good call, added an example. $\endgroup$
    – ZachB
    Oct 12, 2016 at 5:42
  • 1
    $\begingroup$ Consider posting it on packagedata.net too $\endgroup$
    – Szabolcs
    Oct 12, 2016 at 6:41
  • $\begingroup$ Another importer was written just a few days ago, see at the end of comment thread here: mathematica.stackexchange.com/q/127300/12 $\endgroup$
    – Szabolcs
    Oct 12, 2016 at 6:43
  • 1
    $\begingroup$ @JessRiedel You're correct. I'm not on a computer with M right now but can double check in the morning that a fresh install works. What version of M are you using? You can try manually loading this file to register the importer, something like << FileNameJoin[{$BaseDirectory, "SystemFiles", "Formats", "YAML", "Import.m"}]. $\endgroup$
    – ZachB
    Sep 6, 2018 at 2:21
2
$\begingroup$

If you cannot find a YAML importer, consider converting the YAML to JSON and importing that. It'll give you the list of rules you'd expect.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.