TablesawによるJSONデータの基礎

Java でのデータインポート

Anthony Markham

VP Quant Developer

JSONの概要

  • JSON: JavaScript Object Notation
    • 軽量なデータ交換フォーマット
    • Web APIの標準フォーマット
    • キーと値のペア構造

json_image.jpgの画像

Java でのデータインポート

表形式データ

  • 固定された行と列
  • 均一なデータ型
| Name  | Age | City    |
|-------|-----|---------|
| Alice | 30  | Boston  |
| Bob   | 25  | Seattle |

JSON

  • 柔軟なネスト構造
  • 複合データ型と階層
[
  {"name": "Alice", 
      "age": 30, 
      "address": {"city": "Boston", 
                  "state": "MA"}},
  {"name": "Bob", 
      "age": 25, 
      "address": {"city": "Seattle", 
                  "state": "WA"}}
]
Java でのデータインポート

JSONの読み込み

  • シンプルなJSONファイルのみに使用する
// Simple method to read a JSON file
Table products = Table.read().file("products.json");

$$

import tech.tablesaw.io.json.JsonReader;
import tech.tablesaw.io.json.JsonReadOptions;
  • 追加の設定が可能
// Reading a JSON file using JsonReadOptions
JsonReadOptions options = JsonReadOptions.builder("products.json").build();

Table products = new JsonReader().read(options);
Java でのデータインポート

JSONデータへのアクセス

  • Tablesawの標準メソッドが利用可能
// Access columns from JSON data
StringColumn names = table.stringColumn("name");
DoubleColumn prices = table.doubleColumn("price");

// Perform calculations double avgPrice = prices.mean(); String mostExpensive = table .where(prices.isEqualTo(prices.max())) .stringColumn("name").get(0);
Java でのデータインポート

JSONのベストプラクティス - 検証

  • 処理前にJSON構造を検証する
// Validation and error handling
try {
    JsonReadOptions options = JsonReadOptions.builder("data.json").build();
    Table data = new JsonReader().read(options);
    if (data.rowCount() > 0) {
        // Process data
    }
} catch (Exception e) {
    System.err.println("Error reading JSON: " + e.getMessage());
}
if (data.rowCount() == 100) {
    System.out.println("Table has exactly 100 rows - processing data");
}
Java でのデータインポート

JSONのベストプラクティス - 欠損値

  • 欠損値・null値を処理する
// Remove rows with any missing values
data = data.dropRowsWithMissingValues();
  • データ型を分析目的に合わせる
// Convert integer column to double for calculations
data = data.replaceColumn("price", data.intColumn("price").asDoubleColumn());
Java でのデータインポート

さあ、練習しましょう!

Java でのデータインポート

Preparing Video For Download...