Search code examples
c#.netjsonyamlyamldotnet

How to convert YAML to JSON?


I'm looking to convert between a YAML file, and JSON. This was really difficult to find any information on.


Solution

  • If you do not need the features of Json.NET, you can also use the Serializer class directly to emit JSON:

    // now convert the object to JSON. Simple!
    var js = new Serializer(SerializationOptions.JsonCompatible);
    
    var w = new StringWriter();
    js.Serialize(w, o);
    string jsonText = w.ToString();
    

    You can check two working fiddles here: