Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added
- New `csvStringToJsonStringified()` method that converts CSV content directly to a validated JSON string without requiring file I/O
- Improved test coverage for string-based CSV parsing

### Changed
- No breaking changes - all existing APIs remain unchanged
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ show your :heart: and support.
- [Number](#number)
- [Boolean](#boolean)
+ [Encoding](#encoding)
+ [Working with CSV strings directly](#working-with-csv-strings-directly)
* [Chaining Pattern](#chaining-pattern)
- [Development](#development)
- [CI CD github action](#ci-cd-github-action)
Expand Down Expand Up @@ -306,6 +307,30 @@ You can read and decode files with the following encoding:
.getJsonFromCsv(fileInputName);
```

#### Working with CSV strings directly
If you have CSV content as a string (for example, from an API response or test data), you can parse it directly without writing to a file:

```js

// Parse CSV string to array of objects
let csvString = 'firstName;lastName\nJohn;Doe\nJane;Smith';
let jsonArray = csvToJson.csvStringToJson(csvString);
// Output: [{"firstName":"John","lastName":"Doe"},{"firstName":"Jane","lastName":"Smith"}]

// Parse CSV string to JSON string (validated)
let jsonString = csvToJson.csvStringToJsonStringified(csvString);
// Output: "[\n {\n \"firstName\": \"John\",\n \"lastName\": \"Doe\"\n },\n {\n \"firstName\": \"Jane\",\n \"lastName\": \"Smith\"\n }\n]"
```

Both methods support all configuration options through the chaining pattern:

```js
let jsonArray = csvToJson
.fieldDelimiter(',')
.formatValueByType()
.csvStringToJson(csvString);
```

### Chaining Pattern

The exposed API is implemented with the [Method Chaining Pattern](https://en.wikipedia.org/wiki/Method_chaining), which means that multiple methods can be chained, e.g.:
Expand Down
8 changes: 8 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ declare module 'convert-csv-to-json' {
getJsonFromCsv(inputFileName: string): any[];

csvStringToJson(csvString: string): any[];

/**
* Parses a csv string and returns a JSON string (validated)
* @param {csvString} csvString CSV content as string
* @return {string} JSON stringified result
*/
csvStringToJsonStringified(csvString: string): string;

/**
* Parses .csv file and put its content into a file in json format.
* @param {inputFileName} path/filename
Expand Down
12 changes: 12 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,18 @@ exports.csvStringToJson = function(csvString) {
return csvToJson.csvStringToJson(csvString);
};

/**
* Parses a csv string and returns a JSON string (validated)
* @param {csvString} csvString CSV content as string
* @return {string} JSON stringified result
*/
exports.csvStringToJsonStringified = function(csvString) {
if (csvString === undefined || csvString === null) {
throw new Error("csvString is not defined!!!");
}
return csvToJson.csvStringToJsonStringified(csvString);
};

/**
* Parses .csv file and put its content into a file in json format.
* @param {inputFileName} path/filename
Expand Down
7 changes: 7 additions & 0 deletions src/csvToJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ class CsvToJson {
return this.csvToJson(csvString);
}

csvStringToJsonStringified(csvString) {
let json = this.csvStringToJson(csvString);
let jsonStringified = JSON.stringify(json, undefined, 1);
jsonUtils.validateJson(jsonStringified);
return jsonStringified;
}

csvToJson(parsedCsv) {
this.validateInputConfig();
let lines = parsedCsv.split(newLine);
Expand Down
16 changes: 16 additions & 0 deletions test/csvString.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';
let index = require('../index');

describe('csvStringToJsonStringified()', function () {
it('should parse csv content string and return validated JSON string', function () {
let csv = 'firstName;lastName;email;gender;age;birth;zip;registered\nConstantin;Langsdon;[email protected];Male;96;10.02.1965;123;true\nNorah;Raison;[email protected];Female;32.5;10.05.2000;;false\n';
let jsonString = index.csvStringToJsonStringified(csv);
expect(typeof jsonString).toBe('string');
// should be valid JSON
let parsed = JSON.parse(jsonString);
expect(Array.isArray(parsed)).toBe(true);
expect(parsed.length).toBe(2);
expect(parsed[0].firstName).toBe('Constantin');
expect(parsed[1].firstName).toBe('Norah');
});
});