Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is JSON and why is it commonly used in programming?
JSON (JavaScript Object Notation) is a simple text format to store and exchange data. It is easy for humans to read and write, and easy for machines to parse and generate. It is widely used for data exchange between web servers and clients.
Click to reveal answer
beginner
Which C# class is commonly used to read and write JSON files?
The <code>System.Text.Json.JsonSerializer</code> class is commonly used in C# to convert objects to JSON strings and JSON strings back to objects. It helps read from and write to JSON files easily.
Click to reveal answer
intermediate
How do you read a JSON file into a C# object?
You can read a JSON file by first reading the file content as a string using File.ReadAllText, then use JsonSerializer.Deserialize<T>(jsonString) to convert it into a C# object of type T.
Click to reveal answer
intermediate
How do you write a C# object to a JSON file?
Use JsonSerializer.Serialize(object) to convert the object to a JSON string, then write it to a file using File.WriteAllText. This saves the object data in JSON format.
Click to reveal answer
intermediate
What is the importance of matching C# class structure with JSON data?
The C# class properties must match the JSON keys to correctly convert JSON data to objects and vice versa. If they don't match, data may be lost or cause errors during serialization or deserialization.
Click to reveal answer
Which method reads the entire content of a JSON file as a string in C#?
AFile.ReadLine
BFile.ReadAllText
CJsonSerializer.Deserialize
DJsonSerializer.Serialize
✗ Incorrect
File.ReadAllText reads the whole file content as a string, which can then be deserialized.
Which class is used to convert JSON strings to C# objects?
AJsonReader
BFile
CStreamReader
DJsonSerializer
✗ Incorrect
JsonSerializer provides methods to serialize and deserialize JSON data.
What happens if the C# class property names do not match JSON keys during deserialization?
AProperties with no match are ignored
BProgram crashes immediately
CJSON file is deleted
DAll data is loaded correctly
✗ Incorrect
Properties without matching JSON keys are ignored, so data may be missing.
How do you save a C# object as a JSON file?
ASerialize object to string, then write string to file
BUse File.ReadAllText
CUse JsonSerializer.Deserialize
DUse Console.WriteLine
✗ Incorrect
You serialize the object to a JSON string and then write that string to a file.
Which namespace must you include to work with JSON serialization in C#?
ASystem.Net
BSystem.IO
CSystem.Text.Json
DSystem.Collections
✗ Incorrect
System.Text.Json contains the JsonSerializer class for JSON operations.
Explain the steps to read data from a JSON file and convert it into a C# object.
Think about file reading and deserialization.
You got /3 concepts.
Describe how to save a C# object into a JSON file.
Focus on serialization and file writing.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using JSON files in C# programming?
easy
A. To store and exchange data in a simple text format
B. To compile C# code faster
C. To create graphical user interfaces
D. To manage database connections
Solution
Step 1: Understand JSON file usage
JSON files store data in a readable text format that can be shared or saved easily.
Step 2: Identify the correct purpose
Among the options, only storing and exchanging data matches JSON's role.
Final Answer:
To store and exchange data in a simple text format -> Option A
Quick Check:
JSON = data storage and exchange [OK]
Hint: JSON files hold data as text for easy sharing [OK]
Common Mistakes:
Thinking JSON compiles code
Confusing JSON with UI design
Assuming JSON manages databases
2. Which of the following is the correct way to read a JSON file content into a string in C#?
easy
A. string json = JsonSerializer.ReadFile("data.json");
B. string json = File.ReadAllText("data.json");
C. string json = File.ReadJson("data.json");
D. string json = JsonConvert.Read("data.json");
Solution
Step 1: Recall file reading method in C#
The method File.ReadAllText reads all text from a file into a string.
Step 2: Check method correctness
Only File.ReadAllText("data.json") is valid syntax to read JSON as string.
Final Answer:
string json = File.ReadAllText("data.json"); -> Option B
Quick Check:
File.ReadAllText reads file content [OK]
Hint: Use File.ReadAllText to read JSON file content [OK]
Common Mistakes:
Using non-existent methods like ReadJson
Confusing JsonSerializer with file reading
Using JsonConvert without importing Newtonsoft
3. Given the following code, what will be the output?
using System.Text.Json;
var json = "{\"Name\":\"Alice\", \"Age\":30}";
var person = JsonSerializer.Deserialize<Person>(json);
Console.WriteLine(person.Name + " is " + person.Age + " years old.");
public class Person {
public string Name { get; set; }
public int Age { get; set; }
}
medium
A. Alice is 30 years old.
B. Name is Alice, Age is 30
C. System.Text.Json.JsonException
D. null is 0 years old.
Solution
Step 1: Deserialize JSON string to Person object
The JsonSerializer converts the JSON string into a Person object with Name = "Alice" and Age = 30.
Step 2: Print the formatted string
The Console.WriteLine outputs "Alice is 30 years old." by accessing person.Name and person.Age.
Hint: Deserialize JSON then access properties for output [OK]
Common Mistakes:
Expecting JSON string printed directly
Confusing property names or types
Ignoring deserialization step
4. What is wrong with this code snippet that tries to write an object to a JSON file?
using System.Text.Json;
using System.IO;
var person = new Person { Name = "Bob", Age = 25 };
File.WriteAllText("person.json", person.ToString());
public class Person {
public string Name { get; set; }
public int Age { get; set; }
}
medium
A. File.WriteAllText cannot write to JSON files
B. Person class must be static to serialize
C. person.ToString() does not convert the object to JSON format
D. Missing using directive for System.IO
Solution
Step 1: Analyze how object is converted to JSON
Calling person.ToString() returns the class name, not JSON text.
Step 2: Identify correct serialization method
Use JsonSerializer.Serialize(person) to convert the object to JSON string before writing.
Final Answer:
person.ToString() does not convert the object to JSON format -> Option C
Quick Check:
To write JSON, serialize object first [OK]
Hint: Use JsonSerializer.Serialize, not ToString(), to get JSON [OK]
Common Mistakes:
Using ToString() instead of serialization
Assuming WriteAllText can't write JSON
Thinking class must be static to serialize
5. You want to read a JSON file containing a list of products and convert it into a List<Product> in C#. Which code snippet correctly accomplishes this?
public class Product {
public string Name { get; set; }
public double Price { get; set; }
}
// JSON file content example: [{"Name":"Pen","Price":1.5},{"Name":"Book","Price":12.99}]
// Which code correctly reads and deserializes the JSON file?
hard
A. var products = JsonSerializer.Deserialize(File.ReadAllText("products.json"));
products = null;
B. var products = JsonSerializer.Deserialize("products.json");
C. var products = JsonSerializer.Deserialize(File.ReadAllLines("products.json"));
D. var json = File.ReadAllText("products.json");
var products = JsonSerializer.Deserialize(json);
Solution
Step 1: Read entire JSON file as string
Use File.ReadAllText to get the JSON content from the file.
Step 2: Deserialize JSON string to List<Product>
Use JsonSerializer.Deserialize(json) to convert JSON text into a list of Product objects.
Final Answer:
var json = File.ReadAllText("products.json");
var products = JsonSerializer.Deserialize(json); -> Option D
Quick Check:
ReadAllText + Deserialize = correct [OK]
Hint: Read file text first, then deserialize to list [OK]