Working with JSON and XML
Process structured data using JSON and XML in C# applications.
Working with JSON and XML is a free C# Academy lesson on CoddyKit — lesson 2 of 5. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 5 lessons in the course, and your progress syncs across the web and the CoddyKit app.
1
Working with JSON and XML
Welcome to the next lesson! In this lesson, you’ll learn how to work with JSON and XML in C#, two common formats for storing and exchanging data. Let’s get started!

2
What is JSON?
JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write and easy for machines to parse and generate.
Example:
{
"name": "Alice",
"age": 25,
"city": "New York"
}
Tip: JSON is commonly used for APIs and data exchange between applications.
3
Working with JSON in C#
You can use the System.Text.Json namespace to work with JSON in C#. Example:
Key Point: Use JsonSerializer for simple and efficient JSON processing.
using System;
using System.Text.Json;
class Program {
static void Main() {
string json = "{\"name\":\"Alice\",\"age\":25,\"city\":\"New York\"}";
// Deserialize JSON to an object
var person = JsonSerializer.Deserialize<Person>(json);
Console.WriteLine($"Name: {person.Name}, Age: {person.Age}, City: {person.City}");
// Serialize an object to JSON
string newJson = JsonSerializer.Serialize(person);
Console.WriteLine("Serialized JSON: " + newJson);
}
}
class Person {
public string Name { get; set; }
public int Age { get; set; }
public string City { get; set; }
}
4
What is XML?
XML (eXtensible Markup Language) is a flexible text format for structured data. It is commonly used for data storage and configuration files.
Example:
Alice 25 New York
Tip: XML is more verbose than JSON but supports more complex structures and metadata.
5
Working with XML in C#
You can use the System.Xml namespace to work with XML in C#. Example:
Key Point: Use XmlSerializer for converting objects to and from XML.
using System;
using System.Xml.Serialization;
using System.IO;
class Program {
static void Main() {
string xml = "<Person><Name>Alice</Name><Age>25</Age><City>New York</City></Person>";
// Deserialize XML to an object
var serializer = new XmlSerializer(typeof(Person));
using (var reader = new StringReader(xml)) {
var person = (Person)serializer.Deserialize(reader);
Console.WriteLine($"Name: {person.Name}, Age: {person.Age}, City: {person.City}");
}
// Serialize an object to XML
var newPerson = new Person { Name = "Bob", Age = 30, City = "San Francisco" };
using (var writer = new StringWriter()) {
serializer.Serialize(writer, newPerson);
Console.WriteLine("Serialized XML: " + writer.ToString());
}
}
}
public class Person {
public string Name { get; set; }
public int Age { get; set; }
public string City { get; set; }
}
6
JSON vs XML
Here’s a comparison of JSON and XML:
- JSON: Lightweight, easy to read and write, ideal for APIs and modern applications.
- XML: More verbose, supports complex structures, widely used in legacy systems and configurations.
Tip: Choose the format based on your use case and data complexity.
7
Practice Challenge
Create a program that serializes a list of objects to JSON and XML formats and then deserializes them back to objects. Example:
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Xml.Serialization;
using System.IO;
class Program {
static void Main() {
var people = new List<Person> {
new Person { Name = "Alice", Age = 25, City = "New York" },
new Person { Name = "Bob", Age = 30, City = "San Francisco" }
};
// Serialize to JSON
string json = JsonSerializer.Serialize(people);
Console.WriteLine("JSON:\n" + json);
// Serialize to XML
var serializer = new XmlSerializer(typeof(List<Person>));
using (var writer = new StringWriter()) {
serializer.Serialize(writer, people);
Console.WriteLine("XML:\n" + writer.ToString());
}
}
}
public class Person {
public string Name { get; set; }
public int Age { get; set; }
public string City { get; set; }
}
8
9
Best Practices for JSON and XML
Here are some best practices:
- Use JSON for lightweight, human-readable data exchange.
- Use XML for complex structures and metadata-rich configurations.
- Validate data formats to ensure compatibility.
Tip: Use libraries like Newtonsoft.Json for advanced JSON processing when needed.
10
Great Job!
Congratulations! You’ve learned how to work with JSON and XML to store and exchange data in C#. In the next lesson, we’ll explore exception handling to make your programs more robust. Let’s keep coding!

Frequently asked questions
Is the “Working with JSON and XML” lesson free?
Yes — the full text of “Working with JSON and XML” is free to read here on the web, and the C# Academy course includes 5 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “Working with JSON and XML”?
Process structured data using JSON and XML in C# applications. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 5, so you can start here or from the beginning and move at your own pace.
How long does the “Working with JSON and XML” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.