Blog
Wild & Free Tools

Convert YAML to JSON in C# and .NET — Complete Guide

Last updated: February 2026 6 min read
Quick Answer

Table of Contents

  1. Install YamlDotNet
  2. Convert YAML to JSON — Typed Class
  3. Convert Dynamic YAML — No Typed Class
  4. Using Newtonsoft.Json Instead of System.Text.Json
  5. Read a YAML File and Write JSON
  6. Frequently Asked Questions

Converting YAML to JSON in C# requires one NuGet package: YamlDotNet. .NET's standard library does not include a YAML parser. Once YamlDotNet is installed, the pattern is: deserialize YAML into a C# object, then serialize that object to JSON with System.Text.Json (built-in to .NET 6+) or Newtonsoft.Json.

Install YamlDotNet

Add the package to your project:

dotnet add package YamlDotNet

Or via the Package Manager Console:

Install-Package YamlDotNet

YamlDotNet supports YAML 1.1 and 1.2 and works on .NET 6, 7, 8, and .NET Standard 2.0/2.1. It is the dominant YAML library for .NET with tens of millions of downloads.

Approach 1 — Deserialize to a Typed Class (Recommended)

When you know the YAML schema, define a C# class and deserialize directly into it. Then serialize to JSON.

using System;
using System.Text.Json;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;

// Define your config shape
public class ServerConfig
{
    public string Host { get; set; }
    public int Port { get; set; }
    public bool Debug { get; set; }
}

class Program
{
    static void Main()
    {
        var yaml = @"
host: localhost
port: 8080
debug: false
";
        var deserializer = new DeserializerBuilder()
            .WithNamingConvention(CamelCaseNamingConvention.Instance)
            .Build();

        var config = deserializer.Deserialize<ServerConfig>(yaml);

        var json = JsonSerializer.Serialize(config, new JsonSerializerOptions
        {
            WriteIndented = true
        });

        Console.WriteLine(json);
    }
}

Output:

{
  "Host": "localhost",
  "Port": 8080,
  "Debug": false
}

Add [JsonPropertyName("host")] attributes to control output key casing, or configure JsonSerializerOptions.PropertyNamingPolicy.

Sell Custom Apparel — We Handle Printing & Free Shipping

Approach 2 — Convert Dynamic YAML Without a Typed Class

When the YAML structure is not known at compile time, deserialize to a Dictionary<object, object> and recursively convert to a Dictionary<string, object> before JSON serialization.

using System;
using System.Collections.Generic;
using System.Text.Json;
using YamlDotNet.Serialization;

class Program
{
    static object ConvertKeys(object obj)
    {
        if (obj is Dictionary<object, object> dict)
        {
            var result = new Dictionary<string, object>();
            foreach (var kvp in dict)
                result[kvp.Key.ToString()] = ConvertKeys(kvp.Value);
            return result;
        }
        if (obj is List<object> list)
        {
            for (int i = 0; i < list.Count; i++)
                list[i] = ConvertKeys(list[i]);
        }
        return obj;
    }

    static void Main()
    {
        var yaml = @"
server:
  host: localhost
  port: 8080
features:
  - auth
  - logging
";
        var deserializer = new DeserializerBuilder().Build();
        var raw = deserializer.Deserialize<Dictionary<object, object>>(yaml);
        var converted = ConvertKeys(raw);

        var json = JsonSerializer.Serialize(converted, new JsonSerializerOptions
        {
            WriteIndented = true
        });
        Console.WriteLine(json);
    }
}

The ConvertKeys function is the same pattern as Go and Python — YAML parsers produce maps with untyped keys, and JSON serializers require string keys.

Using Newtonsoft.Json (Json.NET)

If your project uses Newtonsoft.Json (Json.NET), the serialization step is the same — just swap the serializer:

using Newtonsoft.Json;

// Replace:
var json = JsonSerializer.Serialize(converted, new JsonSerializerOptions { WriteIndented = true });

// With:
var json = JsonConvert.SerializeObject(converted, Formatting.Indented);

Newtonsoft.Json handles Dictionary<string, object> correctly without additional configuration. Both libraries produce equivalent JSON output for standard YAML content.

Read a YAML File and Write a JSON File

using System.IO;
using System.Text.Json;
using YamlDotNet.Serialization;

var yamlText = File.ReadAllText("config.yaml");

var deserializer = new DeserializerBuilder().Build();
var raw = deserializer.Deserialize<Dictionary<object, object>>(yamlText);
var converted = ConvertKeys(raw); // use function from Approach 2

var json = JsonSerializer.Serialize(converted, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText("config.json", json);
Console.WriteLine("Converted config.yaml -> config.json");

Test Your YAML Without a .NET Project

Paste your YAML into the browser converter and click Convert. See the exact JSON output your C# code should produce — zero setup.

Open Free YAML to JSON Converter

Frequently Asked Questions

How do I convert YAML to JSON in C#?

Install YamlDotNet (dotnet add package YamlDotNet). Deserialize your YAML string using the DeserializerBuilder. If deserializing to a typed class, serialize directly with System.Text.Json.JsonSerializer.Serialize(). If using a dynamic Dictionary, run a recursive ConvertKeys() function to convert all map keys to strings first, then serialize.

Does .NET have a built-in YAML parser?

No. .NET's standard library includes System.Text.Json for JSON but has no YAML support. YamlDotNet is the standard NuGet package for YAML parsing in .NET. Install it with: dotnet add package YamlDotNet.

Why does JsonSerializer fail with "Dictionary requires string key" when converting YAML?

YamlDotNet deserializes YAML maps as Dictionary with untyped keys. System.Text.Json requires Dictionary. Fix: write a recursive ConvertKeys() function that walks the deserialized object and converts all dictionary keys to strings before calling JsonSerializer.

Should I use System.Text.Json or Newtonsoft.Json for YAML to JSON conversion in .NET?

Either works. System.Text.Json is built into .NET 6+ and has no extra dependencies. Newtonsoft.Json is still dominant in older .NET projects and has more flexible configuration. If your project already uses one of them, use that. For new projects on .NET 6+, System.Text.Json is the default recommendation.

Andrew Walsh
Andrew Walsh Developer Tools & API Writer

Andrew worked as a developer advocate at two SaaS startups writing API documentation used by thousands of engineers.

More articles by Andrew →
Launch Your Own Clothing Brand — No Inventory, No Risk