Tablesaw로 JSON 데이터 기본

Java에서 데이터 가져오기

Anthony Markham

VP Quant Developer

JSON 소개

  • JSON: JavaScript Object Notation
    • 가벼운 데이터 교환 형식
    • 웹 API 표준 형식
    • 키-값 쌍 구조

json 이미지

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...