JSON data basics with Tablesaw

Importing Data in Java

Anthony Markham

VP Quant Developer

JSON introduction

  • JSON: JavaScript Object Notation
    • Lightweight data interchange format
    • Web API standard format
    • Key-value pair structure

json_image.jpg

Importing Data in Java

Tabular data

  • Fixed rows and columns
  • Tabular: Uniform data types
| Name  | Age | City    |
|-------|-----|---------|
| Alice | 30  | Boston  |
| Bob   | 25  | Seattle |

JSON

  • Flexible nested structures
  • Mixed data types and hierarchies
[
  {"name": "Alice", 
      "age": 30, 
      "address": {"city": "Boston", 
                  "state": "MA"}},
  {"name": "Bob", 
      "age": 25, 
      "address": {"city": "Seattle", 
                  "state": "WA"}}
]
Importing Data in Java

Reading JSON

  • Use only for simple JSON files
// 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;
  • Offers additional configuration
// Reading a JSON file using JsonReadOptions
JsonReadOptions options = JsonReadOptions.builder("products.json").build();

Table products = new JsonReader().read(options);
Importing Data in Java

Accessing JSON data

  • Familiar Tablesaw methods work
// 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);
Importing Data in Java

Best JSON practices - validation

  • Validate JSON structure before processing
// 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");
}
Importing Data in Java

Best JSON practices - missing values

  • Handle missing/null values
// Remove rows with any missing values
data = data.dropRowsWithMissingValues();
  • Match data types to analysis needs
// Convert integer column to double for calculations
data = data.replaceColumn("price", data.intColumn("price").asDoubleColumn());
Importing Data in Java

Let's practice!

Importing Data in Java

Preparing Video For Download...