matiso

matiso

Convert Elixir maps or structs to YAML

I am building an API that needs to allow the user to download a yaml config file for an external application.
I’m not sure if I’m blind, or there is no Elixir library that converts a map (or struct) into YAML? All I can find is how to decode YAML and parse it into an Elixir data structure, but not the other way around.

Most Liked

michalmuskala

michalmuskala

Valid JSON is valid YAML. Any conforming YAML parser should be able to parse JSON. So if you don’t have any specific needs for the YAML format, doing something like:

"---\n" <> Poison.encode!(data)

will produce something any YAML parser will be able to process.

yoran

yoran

@50kudos For what it’s worth, I found myself in a similar situation. I could parse YAML with yaml-elixir but didn’t have a way to write. So I wrote a short YAML writer myself.

def to_yaml(yaml_map, indentation \\ "") do
    yaml_map
    |> Map.keys
    |> Enum.map(
      fn key ->
        value = Map.fetch!(yaml_map, key)
        cond do
          is_bitstring(value) -> "#{indentation}#{key}: #{value}"
          is_number(value) -> "#{indentation}#{key}: #{value}"
          is_map(value) -> "#{indentation}#{key}:\n#{to_yaml(value, "#{indentation}  ")}"
        end
      end
    )
    |> Enum.join("\n")
end

You use it as follows:

YamlElixir.read_from_file!("file.yaml")
|> Map.put(:some_key, %{some_nested_key: 123})
|> to_yaml

It’s not perfect but it suits my purposes (reading and writing the app.yaml configuration file in Google App Engine).

Thought I’d share to help others who come here while searching, like I did!

Last Post!

7rans

7rans

It actually is standardized, but implementors have taken liberties with it, and some have been reluctant to updated from 1.1 to 1.2. Yes, YAML is complex compared to JSON, but it was designed to cover more use cases. The alternative might have led to multiple ad-hoc solutions sprouting up for those additional features.

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130286 1222
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54006 488
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New

We're in Beta

About us Mission Statement