Why some values are quoted
A plain YAML value has no type marker; the parser guesses the type from the text, and YAML 1.1 and 1.2 guess differently. The converter quotes every string that some parser would read as something else:
| JSON string | Unquoted, YAML 1.1 (PyYAML) reads | Unquoted, YAML 1.2 core schema reads |
|---|---|---|
"NO", "yes", "off" | false, true, false | the string |
"0755" | 493 (octal) | 755 |
"1:30" | 90 (base 60) | the string |
"2024-05-01" | a date | the string |
"null", "~" | null | null |
"true", "12" | true, 12 | true, 12 |
key "on" | the boolean true as the key | the string |
This is the "Norway problem": the country code NO in a YAML 1.1 file is false. It is also why a GitHub Actions workflow converted here starts with "on":. Strings that contain : or #, or start with a character YAML uses as syntax (-, *, &, !, [, {, |, >, %, @), are quoted for the same reason.
Numbers, text and empty values
- Exponents are rewritten: JSON
1e3becomes1.0e+3, because YAML 1.1 reads1e3as a string. - Multi-line strings become literal blocks (
|) that keep every line break. Text with carriage returns, tabs at the start of a line or control characters is written in double quotes with escapes instead. - Empty objects and arrays are written as
{}and[], andnullstaysnull. - Duplicate keys are not allowed in YAML. The last value is kept, as most JSON parsers do, and the converter tells you.
Convert JSON to YAML in C#
YamlDotNet serializes dictionaries, lists and primitives, so turn the JsonNode into those first:
// dotnet add package YamlDotNet
using System.Text.Json;
using System.Text.Json.Nodes;
using YamlDotNet.Serialization;
static string JsonToYaml(string json)
{
var serializer = new SerializerBuilder()
.WithQuotingNecessaryStrings(quoteYaml1_1Strings: true) // quotes "yes", "on", "0755"
.Build();
return serializer.Serialize(ToPlain(JsonNode.Parse(json)));
}
// YamlDotNet writes dictionaries, lists and primitives; it does not know JsonNode.
static object? ToPlain(JsonNode? node) => node switch
{
JsonObject o => o.ToDictionary(p => p.Key, p => ToPlain(p.Value)),
JsonArray a => a.Select(ToPlain).ToList(),
JsonValue v => v.GetValueKind() switch
{
JsonValueKind.String => v.GetValue<string>(),
JsonValueKind.Number when v.TryGetValue(out long l) => l,
JsonValueKind.Number when v.TryGetValue(out decimal m) => m,
JsonValueKind.Number => v.GetValue<double>(),
_ => v.GetValue<bool>(),
},
_ => null, // JSON null
};
Tested on .NET 10 with YamlDotNet 18.1.0. Its output reads back correctly in both YamlDotNet and PyYAML, with these exceptions we found: strings with tabs, carriage returns or other control characters (it may fold or drop them), the strings = and << and dates such as 2024-05-01 (left unquoted; YAML 1.1 parsers read them as other types), and numbers with an exponent (1e3, a string in YAML 1.1). JsonNode also throws on duplicate keys. For those inputs, use this page.
FAQ
Why are some values in quotes?
Unquoted, a parser would read them as another type: in YAML 1.1, NO is false, 0755 is 493 and 1:30 is 90. Quotes keep them strings in every parser.
Is all JSON valid YAML?
Almost: YAML 1.2 is designed as a superset of JSON, so a YAML 1.2 parser reads most JSON as it is. Block-style YAML is easier to read and diff, and it avoids the cases where YAML 1.1 parsers disagree.
Can ASP.NET Core read YAML configuration?
Not out of the box: the built-in providers read JSON, XML, INI, environment variables and command-line arguments. For YAML, add a community configuration provider, or keep appsettings.json and use YAML for deployment files.
Why are some values in quotes in the YAML?
Without quotes, a YAML parser would read them as another type. In YAML 1.1 (PyYAML, and tools built on it) NO and yes are booleans, 0755 is the octal number 493 and 1:30 is 90. Quoting keeps them strings in every parser.
How do I convert JSON to YAML in C#?
Parse the JSON with JsonNode, turn it into dictionaries and lists, and serialize them with YamlDotNet's SerializerBuilder and WithQuotingNecessaryStrings(quoteYaml1_1Strings: true). The page has the full code.
Can I convert YAML back to JSON?
Yes, with a YAML parser such as YamlDotNet. Its untyped deserializer returns every value as a string, so deserialize into typed classes to get numbers and booleans back, then serialize with System.Text.Json. This page converts JSON to YAML only.
Is my JSON uploaded?
No. The converter runs in your browser.