Two of the most widely used data formats in contemporary web development are YAML and JSON.
Both facilitate the reading, sharing, and storing of structured data by your apps. However, their functions are distinct.
You will frequently encounter both if you work with PHP or ASP.NET. Let's examine how to apply them successfully in each setting.
JSON (JavaScript Object Notation) is the foundation of web APIs. It is lightweight, easy to read, and included in most modern languages.
PHP works with JSON simply using built-in functions.
Read JSON
$jsonData = file_get_contents('config.json');
$data = json_decode($jsonData, true);
echo $data['database']['host'];
Write JSON
$data = ['name' => 'Muazzam', 'role' => 'Software Engineer'];
file_put_contents('output.json', json_encode($data, JSON_PRETTY_PRINT));
ASP.NET Core uses JSON everywhere, especially in appsettings.json.
Read JSON
using System.Text.Json;
string json = File.ReadAllText("config.json");
var data = JsonSerializer.Deserialize>(json);
Console.WriteLine(data["AppName"]);
Write JSON
var data = new { Name = "Muazzam", Role = "Software Engineer" };
string json = JsonSerializer.Serialize(data, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText("output.json", json);
appsettings.jsonYAML (YAML Ain’t Markup Language) focuses on readability.
It’s used heavily in frameworks and DevOps tools because developers can scan and edit it easily.
PHP doesn’t support YAML natively, but the Symfony YAML component makes it simple.
Install
composer require symfony/yaml
use Symfony\Component\Yaml\Yaml;
$data = Yaml::parseFile('config.yaml');
echo $data['database']['host'];
use Symfony\Component\Yaml\Yaml;
$data = ['name' => 'Muazzam', 'role' => 'Software Engineer'];
file_put_contents('output.yaml', Yaml::dump($data, 2, 4));
ASP.NET doesn’t include YAML support by default, but you can use YamlDotNet.
Install
dotnet add package YamlDotNet
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
var yaml = File.ReadAllText("config.yaml");
var deserializer = new DeserializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.Build();
var data = deserializer.Deserialize>(yaml);
Console.WriteLine(data["database"]);
var serializer = new SerializerBuilder().Build();
var yaml = serializer.Serialize(new { Name = "Muazzam", Role = "Developer" });
File.WriteAllText("output.yaml", yaml);
| Scenario | Choose JSON | Choose YAML |
|---|---|---|
| API communication | ✔ | |
| Web app configuration | ✔ | |
| Human-editable config | ✔ | |
| CI/CD or DevOps | ✔ | |
| Cross-language data exchange | ✔ |
JSON is fast, compact, and great for APIs and structured app data.
YAML is more readable, flexible, and preferred for configuration files.
PHP supports both easily through built-in or external libraries.
ASP.NET integrates JSON natively and supports YAML via YamlDotNet.
Use JSON when you care about efficiency and machine readability.
Use YAML when you want clarity and human-friendly configuration.
Both formats complement each other and belong in every developer’s toolkit.