JSON DTO Exercise
Part 1
Answer the following questions: link for help
- What does JSON stand for?
- What is the difference between JSON and XML?
- For what is JSON generally used for?
- Write down the 6 data types in JSON.
- Write down the 4 JSON syntax rules.
Part 2
- Create a new file called
account.json
- Create a JSON object with the following properties in account.json (the notation is pseudo code):
`firstName` (string)
`lastName` (string)
`birthDate` (string)
`address` (object)
- `street` (string)
- `city` (string)
- `zipCode` (integer)
`account` (object)
- `id` (string)
- `balance` (string)
- `isActive` (boolean)
- Wrap the JSON object in an array
- Add 5 more accounts to the array
- Save the file
- Open the file in a browser and verify that it is valid JSON. You do this by paste the absolute path to the file in the browser address bar. For example: `file:///Users/username/Desktop/account.json
Part 3
- Open a Java project in your IDE and create classes for the JSON object you created in part 2.
- Create a method that can read the JSON object from the file and return an array of Account objects.
- You can use a JAVA library to convert the JSON object to a JAVA object or vice versa. For example Jackson.
- Create a DTO class that has the following properties:
`fullName` (string)
`city` (string)
`zipCode` (string)
`isActive` (string)
- Create a method that can print out the DTO objects in the array in a nice format.
Hint! - if you need help reading the JSON file in Java
Here’s a Java method called `readJsonFromFile` that reads a JSON file from the resources folder into a `String`:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.stream.Collectors;
public class JsonFileReader {
public static String readJsonFromFile(String filename) {
// Get the resource file from the classpath
InputStream inputStream = JsonFileReader.class.getClassLoader().getResourceAsStream(filename);
if (inputStream == null) {
throw new IllegalArgumentException("File not found: " + filename);
}
// Read the file content into a string
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
return reader.lines().collect(Collectors.joining("\n"));
} catch (IOException e) {
throw new RuntimeException("Failed to read the file: " + filename, e);
}
}
}
Explanation
InputStream
: This reads the file as a stream from the resources folder usinggetResourceAsStream()
.BufferedReader
: Wraps the input stream for efficient reading.Collectors.joining("\n")
: Collects the lines of the file into a single string, separated by newlines.- Exception Handling: If the file is not found, an
IllegalArgumentException
is thrown. If an I/O error occurs, aRuntimeException
is thrown.
How to use
Place your JSON file in the resources
folder, and call the method like this:
String jsonString = JsonFileReader.readJsonFromFile("data.json");
System.out.println(jsonString);
This will read the contents of the data.json
file into a String
. Make sure the JSON file is in the resources
folder within the project structure.