JSON data analysis
With the product catalog loaded, the marketing team has requested a pricing report. They want to know the average price, identify the most expensive product, and see how products are distributed across categories.
The good news: once JSON data is in a Tablesaw table, you can use the same column operations you already know.
The JsonReader, JsonReadOptions, and Table classes have been imported, and the products.json file contains the name, price, and category columns.
Bu egzersiz
Importing Data in Java
kursunun bir parçasıdırEgzersiz talimatları
- Complete the JSON read to load the products table.
- Calculate the average price using a column method.
- Find the maximum price in the dataset.
Uygulamalı interaktif egzersiz
Bu örnek kodu tamamlayarak bu egzersizi bitirin.
public class JSONAnalysis {
public static void main(String[] args) {
// Complete the JSON read
JsonReadOptions options = JsonReadOptions.builder("products.json").build();
Table products = new JsonReader().____(options);
// Calculate average price
double avgPrice = products.doubleColumn("price").____();
// Find maximum price
DoubleColumn priceCol = products.doubleColumn("price");
double maxPrice = ____.____();
String mostExpensive = products
.where(priceCol.isEqualTo(maxPrice))
.stringColumn("name").get(0);
Table categoryCount = products.countBy("category");
System.out.println("Product Analytics Report:");
System.out.println("========================");
System.out.printf("Average Price: $%.2f%n", avgPrice);
System.out.println("Most Expensive: " + mostExpensive + " ($" + maxPrice + ")");
System.out.println("\nCategory Distribution:");
System.out.println(categoryCount.print());
}
}