
How to convert CSV to JSON with Node.js API.
CSV(Comma-Separated Values) er et af de mest almindeligt anvendte formater til lagring af tabeldata. Men når man arbejder med API’er, applikationer eller moderne webmiljøer, er JSON(JavaScript Object Notation) at foretrække på grund af sin hierarkiske og let parsebare struktur.
Denne artikel guider dig gennem konvertering af CSV til JSON ved hjælp af REST API, designet til problemfri datatransformation i skybaserede miljøer.
- Node.js API til CSV til JSON konvertering
- Convert CSV to JSON in Node.js
- CSV til JSON-konvertering med cURL
Node.js API til CSV til JSON konvertering
Aspose.Cells Cloud SDK for Node.js gør det muligt for dig at konvertere filer som CSV til JSON hurtigt, nøjagtigt og uden behov for nogen lokal softwareinstallation.
Nøglefunktioner
- Konverter regneark og CSV-filer til JSON.
- Opbevar struktur og datatyper under konvertering.
- Upload, behandl og download filer ved hjælp af cloud API’er.
- Understøtter over 50 filformater.
Kom i gang
- Installer SDK via npm:
npm install asposecellscloud --save
- Opsæt legitimationsoplysninger:
Opret en gratis konto på Aspose.Cloud Dashboard og få din Client ID og Client Secret. For yderligere oplysninger kan du overveje at besøge quick start artiklen.
Convert CSV to JSON in Node.js
Her er en trin-for-trin guide til at konvertere CSV til JSON programmæssigt ved hjælp af Node.js:
Trin 1: Initialiser API’en:
const { CellsApi, PostWorkbookSaveAsRequest, SaveOptions } = require("asposecellscloud");
const cellsApi = new CellsApi("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET");
Trin 2: Upload CSV-filen til Cloud Storage:
const fs = require("fs");
const path = require("path");
const filePath = path.resolve("sample.csv");
await cellsApi.uploadFile("sample.csv", fs.createReadStream(filePath));
Trin 3: Konfigurer og send konverteringsanmodningen:
const saveOptions = {
SaveFormat: "JSON"
};
const request = new PostWorkbookSaveAsRequest({
name: "sample.csv",
newfilename: "converted/output.json",
saveOptions: saveOptions,
isAutoFitRows: true,
isAutoFitColumns: true
});
await cellsApi.postWorkbookSaveAs(request);
console.log("CSV successfully converted to JSON.");
const { CellsApi, CellsSaveAs_PostDocumentSaveAsRequest,UploadFileRequest} = require("asposecellscloud");
// Få dit ClientId og ClientSecret fra https://dashboard.aspose.cloud (gratis registrering kræves).
const YOUR_CLIENT_ID = "XXXXXXX-XXXXX-4ebe-bff4-f5a14a4b6466";
const YOUR_CLIENT_SECRET = "XXXXXXXXXXXXX8b067a8b1cb625a2ea8e";
const { CellsApi, PostWorkbookSaveAsRequest, SaveOptions } = require("asposecellscloud");
const cellsApi = new CellsApi("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET");
const fs = require("fs");
const path = require("path");
const filePath = path.resolve("sample.csv");
// upload the input CSV to cloud storage.
await cellsApi.uploadFile("sample.csv", fs.createReadStream(filePath));
const saveOptions = {
SaveFormat: "JSON"
};
const request = new PostWorkbookSaveAsRequest({
name: "sample.csv",
newfilename: "converted/output.json",
saveOptions: saveOptions,
isAutoFitRows: true,
isAutoFitColumns: true
});
// initiate the CSV to JSON conversion
await cellsApi.postWorkbookSaveAs(request);
console.log("CSV successfully converted to JSON.");

En forhåndsvisning af CSV til JSON konvertering.
CSV til JSON-konvertering med cURL
Hvis du foretrækker kommandolinjens dygtighed, så er det en problemfri løsning at udføre CSV til JSON konvertering ved hjælp af cURL-kommandoer. Denne tilgang tilbyder en hurtig og scriptbar metode uden viden om programmeringssprog.
Step 1 – Generer adgangstoken:
Det første skridt i denne tilgang er at generere en JWT adgangstoken:
curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"
Step 2 – Udfør CSV til JSON-konvertering:
curl -v "https://api.aspose.cloud/v3.0/cells/{sourceFile}/SaveAs?newfilename={resultantFile}&isAutoFitRows=true&isAutoFitColumns=true&checkExcelRestriction=false" \
-X POST \
-H "accept: application/json" \
-H "authorization: Bearer {accessToken}" \
-H "Content-Type: application/json" \
-d "{ \"SaveFormat\": \"JSON\", \"ClearData\": true, \"CreateDirectory\": true, \"EnableHTTPCompression\": true, \"RefreshChartCache\": true, \"SortNames\": true, \"ValidateMergedAreas\": true}"
Erstat sourceFile
med navnet på input CSV-fil, resultantFile
med JSON-filen og accessToken
med JWT adgangstoken genereret ovenfor.
- De prøvefiler, der blev brugt i ovenstående eksempel, kan downloades fra:
Prøv gratis online CSV til JSON konverter.
Oplev API’en i aktion uden at skrive en eneste kode! Prøv CSV to JSON Converter Online drevet af Aspose.Cells Cloud.

Gratis online CSV til JSON konverteringsapp.
📚 Nyttige ressourcer
Konklusion
Uanset om du behandler data til en webapp, eksporterer regneark til API-forbrug, eller moderniserer dine datapipelines, giver Aspose.Cells Cloud SDK til Node.js en hurtig og pålidelig løsning til CSV til JSON konvertering. Brug REST API’en for fuld integration eller vælg cURL til hurtige og scriptbare opgaver.
Transformér dine CSV-filer til struktureret JSON med blot få linjer kode — og få dine data til at arbejde smartere.
🔗 Relaterede Artikler
Vi anbefaler stærkt at besøge følgende blogs: