0
0
C Sharp (C#)programming~10 mins

Working with JSON files in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Working with JSON files
Start Program
Read JSON File
Parse JSON to Object
Use Object Data
Modify Object (optional)
Serialize Object to JSON
Write JSON to File
End Program
The program reads a JSON file, converts it to an object, optionally modifies it, then writes it back as JSON.
Execution Sample
C Sharp (C#)
using System.Text.Json;

string json = File.ReadAllText("data.json");
var person = JsonSerializer.Deserialize<Person>(json);
person.Age += 1;
string newJson = JsonSerializer.Serialize(person);
File.WriteAllText("data.json", newJson);
This code reads a JSON file, deserializes it into a Person object, increases the age by 1, then writes the updated JSON back.
Execution Table
StepActionCode LineVariable StateOutput/File Content
1Read JSON file contentstring json = File.ReadAllText("data.json");json = '{"Name":"Alice","Age":30}'File content read as string
2Deserialize JSON to Person objectvar person = JsonSerializer.Deserialize<Person>(json);person = Person{Name="Alice", Age=30}No output
3Increase person's age by 1person.Age += 1;person = Person{Name="Alice", Age=31}No output
4Serialize Person object to JSON stringstring newJson = JsonSerializer.Serialize(person);newJson = '{"Name":"Alice","Age":31}'No output
5Write updated JSON string to fileFile.WriteAllText("data.json", newJson);person unchangedFile content updated to '{"Name":"Alice","Age":31}'
6End programEndperson unchangedFile saved with updated JSON
💡 Program ends after writing updated JSON back to file.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5Final
jsonnull{"Name":"Alice","Age":30}{"Name":"Alice","Age":30}{"Name":"Alice","Age":30}{"Name":"Alice","Age":30}{"Name":"Alice","Age":30}{"Name":"Alice","Age":30}
personnullnullPerson{Name="Alice", Age=30}Person{Name="Alice", Age=31}Person{Name="Alice", Age=31}Person{Name="Alice", Age=31}Person{Name="Alice", Age=31}
newJsonnullnullnullnull{"Name":"Alice","Age":31}{"Name":"Alice","Age":31}{"Name":"Alice","Age":31}
Key Moments - 3 Insights
Why do we need to deserialize JSON before using it?
Because JSON is a text format, deserialization converts it into a C# object we can work with, as shown in step 2 of the execution_table.
What happens if we forget to write the updated JSON back to the file?
The file will not reflect changes made to the object in memory. Step 5 shows writing back is necessary to save changes.
Why do we serialize the object again before writing to the file?
Serialization converts the C# object back into JSON text format, which is needed to save it as a file, as shown in step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'person.Age' after step 3?
A31
B30
C29
Dnull
💡 Hint
Check the 'Variable State' column at step 3 in the execution_table.
At which step is the JSON string first read from the file?
AStep 2
BStep 4
CStep 1
DStep 5
💡 Hint
Look at the 'Action' column to find when the file content is read.
If we skip serialization (step 4), what will happen when writing to the file in step 5?
AThe file will be empty
BThe file will contain the object directly (invalid format)
CThe file will contain the old JSON
DThe program will crash
💡 Hint
Consider what File.WriteAllText expects as input and what happens if we pass an object instead of a string.
Concept Snapshot
Working with JSON files in C#:
- Read JSON text from file with File.ReadAllText
- Convert JSON text to object using JsonSerializer.Deserialize<T>
- Modify object properties as needed
- Convert object back to JSON text with JsonSerializer.Serialize
- Write JSON text back to file with File.WriteAllText
Always deserialize before use and serialize before saving.
Full Transcript
This example shows how to work with JSON files in C#. First, the program reads the JSON content from a file as a string. Then it converts this JSON string into a C# object using deserialization. After that, it modifies the object's data, for example increasing the age by one. Next, it converts the updated object back into a JSON string by serialization. Finally, it writes this new JSON string back to the file, saving the changes. This process ensures that the program can read, use, and update JSON data stored in files.