Get startedGet started for free

JSON validation

Your product analysis is working well, and the team wants to deploy it as an automated daily report. But in production, JSON files might be missing, corrupted, or arrive in unexpected formats. Before going live, you need to add error handling so the application fails gracefully.

The JsonReader, JsonReadOptions, and Table classes have been imported for you.

This exercise is part of the course

Importing Data in Java

View Course

Exercise instructions

  • Add a try block to wrap the JSON loading code.
  • Add a catch block to handle any exceptions.
  • Print the error message if loading fails.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

public class JSONValidation {
    public static void main(String[] args) {
        // Add try block for JSON loading
        ____ {
            JsonReadOptions options = JsonReadOptions.builder("products.json").build();
            Table products = new JsonReader().read(options);
            
            System.out.println("Successfully loaded " + products.rowCount() + " products");
            System.out.println(products.first(3));
        // Add catch block for exceptions
        } ____ (Exception e) {
            // Print error message
            System.err.println("Error reading JSON: " + e.____());
        }
    }
}
Edit and Run Code