Convertir CSV a JSON con Node.js a través de C++

Convertir CSV a JSON

Aspose.Cells admite la conversión de CSV a JSON. Para esto, la API proporciona las clases ExportRangeToJsonOptions y JsonUtility. La clase ExportRangeToJsonOptions ofrece las opciones para exportar rango a JSON. La clase ExportRangeToJsonOptions tiene las siguientes propiedades.

La clase JsonUtility exporta el JSON utilizando las opciones de exportación configuradas con la clase ExportRangeToJsonOptions.

El siguiente ejemplo de código demuestra el uso de las clases ExportRangeToJsonOptions y JsonUtility para cargar el archivo CSV fuente e imprimir la salida JSON en la consola.

Código de muestra

const path = require("path");
const AsposeCells = require("aspose.cells.node");

// Source directory
const sourceDir = path.join(__dirname, "data");

const loadOptions = new AsposeCells.LoadOptions(AsposeCells.LoadFormat.Csv);
// Load CSV file
const filePath = path.join(sourceDir, "SampleCsv.csv");
const workbook = new AsposeCells.Workbook(filePath, loadOptions);
const lastCell = workbook.getWorksheets().get(0).getCells().getLastCell();

// Set JsonSaveOptions
const jsonSaveOptions = new AsposeCells.JsonSaveOptions();
const range = workbook.getWorksheets().get(0).getCells().createRange(0, 0, lastCell.getRow() + 1, lastCell.getColumn() + 1);
const data = AsposeCells.JsonUtility.exportRangeToJson(range, jsonSaveOptions);

// Print JSON
console.log(data);

Salida de la consola

[
{
"id": 1,
"language": "Java",
"edition": "third",
"author": "Herbert Schildt",
"streetAddress": 126,
"city": "San Jone",
"state": "CA",
"postalCode": 394221
},
{
"id": 2,
"language": "C++",
"edition": "second",
"author": "EAAAA",
"streetAddress": 126,
"city": "San Jone",
"state": "CA",
"postalCode": 394221
},
{
"id": 3,
"language": ".Net",
"edition": "second",
"author": "E.Balagurusamy",
"streetAddress": 126,
"city": "San Jone",
"state": "CA",
"postalCode": 394221
}
]