Importación de datos en Java
Anthony Markham
VP Quant Developer

| Nombre | Edad | Ciudad |
|--------|------|---------|
| Alice | 30 | Boston |
| Bob | 25 | Seattle |
[
{"name": "Alice",
"age": 30,
"address": {"city": "Boston",
"state": "MA"}},
{"name": "Bob",
"age": 25,
"address": {"city": "Seattle",
"state": "WA"}}
]
// Método simple para leer un archivo JSON
Table products = Table.read().file("products.json");
$$
import tech.tablesaw.io.json.JsonReader;
import tech.tablesaw.io.json.JsonReadOptions;
// Leer un archivo JSON usando JsonReadOptions JsonReadOptions options = JsonReadOptions.builder("products.json").build();Table products = new JsonReader().read(options);
Tablesaw funcionan// Acceder a columnas de datos JSON StringColumn names = table.stringColumn("name"); DoubleColumn prices = table.doubleColumn("price");// Realizar cálculos double avgPrice = prices.mean(); String mostExpensive = table .where(prices.isEqualTo(prices.max())) .stringColumn("name").get(0);
// Validación y manejo de errores
try {
JsonReadOptions options = JsonReadOptions.builder("data.json").build();
Table data = new JsonReader().read(options);
if (data.rowCount() > 0) {
// Procesar datos
}
} catch (Exception e) {
System.err.println("Error al leer JSON: " + e.getMessage());
}
if (data.rowCount() == 100) {
System.out.println("La tabla tiene exactamente 100 filas - procesando datos");
}
// Eliminar filas con valores faltantes
data = data.dropRowsWithMissingValues();
// Convertir columna de enteros a doble para cálculos
data = data.replaceColumn("price", data.intColumn("price").asDoubleColumn());
Importación de datos en Java