{"id":13402807,"url":"https://github.com/iuccio/csvToJson","last_synced_at":"2025-03-14T08:31:09.612Z","repository":{"id":40666783,"uuid":"90399226","full_name":"iuccio/csvToJson","owner":"iuccio","description":"Convert CSV file to JSON","archived":false,"fork":false,"pushed_at":"2024-09-08T11:54:44.000Z","size":392,"stargazers_count":225,"open_issues_count":8,"forks_count":43,"subscribers_count":8,"default_branch":"master","last_synced_at":"2024-10-30T01:03:07.281Z","etag":null,"topics":["csv","csv-parser","csvparser","csvtojson","javascript","json","node","nodejs","npm","npm-module","npm-package","parser","parsing","typescript"],"latest_commit_sha":null,"homepage":"https://iuccio.github.io/csvToJson/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/iuccio.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-05-05T17:10:47.000Z","updated_at":"2024-10-29T04:44:14.000Z","dependencies_parsed_at":"2024-01-16T06:10:52.836Z","dependency_job_id":"b1018d92-e6d5-4d20-944e-98b91da7da43","html_url":"https://github.com/iuccio/csvToJson","commit_stats":{"total_commits":139,"total_committers":10,"mean_commits":13.9,"dds":0.3165467625899281,"last_synced_commit":"d7475866348ada8b4dca7bd71fb0064c96d69f4e"},"previous_names":[],"tags_count":72,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iuccio%2FcsvToJson","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iuccio%2FcsvToJson/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iuccio%2FcsvToJson/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iuccio%2FcsvToJson/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/iuccio","download_url":"https://codeload.github.com/iuccio/csvToJson/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242864305,"owners_count":20197689,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csv","csv-parser","csvparser","csvtojson","javascript","json","node","nodejs","npm","npm-module","npm-package","parser","parsing","typescript"],"created_at":"2024-07-30T19:01:21.099Z","updated_at":"2025-03-14T08:31:09.591Z","avatar_url":"https://github.com/iuccio.png","language":"JavaScript","readme":"# CSVtoJSON \n[![Node CI](https://github.com/iuccio/csvToJson/actions/workflows/ci-cd.yml/badge.svg?branch=master)](https://github.com/iuccio/csvToJson/actions/workflows/ci-cd.yml)\n![CodeQL](https://github.com/iuccio/csvToJson/actions/workflows/codeql-analysis.yml/badge.svg)\n[![Known Vulnerabilities](https://snyk.io/test/github/iuccio/csvToJson/badge.svg)](https://snyk.io/test/github/iuccio/csvToJson)\n[![Code Climate](https://codeclimate.com/github/iuccio/csvToJson/badges/gpa.svg)](https://codeclimate.com/github/iuccio/csvToJson)\n[![NPM Version](https://img.shields.io/npm/v/convert-csv-to-json.svg)](https://npmjs.org/package/convert-csv-to-json)\n![NodeJS Version](https://img.shields.io/badge/nodeJS-%3E=14.x-brightgreen.svg)\n[![Downloads](https://img.shields.io/npm/dm/convert-csv-to-json.svg)](https://npmjs.org/package/convert-csv-to-json)\n[![NPM total downloads](https://img.shields.io/npm/dt/convert-csv-to-json.svg?style=flat)](https://npmjs.org/package/convert-csv-to-json)\n\n![TypeScript](https://img.shields.io/badge/typescript-%23007ACC.svg?style=for-the-badge\u0026logo=typescript\u0026logoColor=white)\n![JavaScript](https://img.shields.io/badge/javascript-%23323330.svg?style=for-the-badge\u0026logo=javascript\u0026logoColor=%23F7DF1E)\n\n**This project is not dependent on others packages or libraries.**\n\nThis repository uses [![GitHub Action  - iuccio/npm-semantic-publish-action@latest](https://img.shields.io/badge/GitHub_Action_-iuccio%2Fnpm--semantic--publish--action%40latest-2ea44f)](https://github.com/marketplace/actions/npm-semver-publish)\n\nFollow [me](https://github.com/iuccio), and consider starring the project to\nshow your :heart: and support.\n\n## Table of Contents\n\n\u003c!-- toc --\u003e\n\n- [Description](#description)\n- [Support for JS \u0026 TS](#support-for-js--ts)\n- [Prerequisites](#prerequisites)\n- [Install npm *convert-csv-to-json package*](#install-npm-convert-csv-to-json-package)\n  * [Install](#install)\n  * [Usage](#usage)\n    + [Generate JSON file](#generate-json-file)\n    + [Generate Array of Object in JSON format](#generate-array-of-object-in-json-format)\n    + [Generate Object with sub array](#generate-object-with-sub-array)\n    + [Define field delimiter](#define-field-delimiter)\n    + [Support Quoted Fields](#support-quoted-fields)\n    + [Index header](#index-header)\n    + [Empty rows](#empty-rows)\n    + [Format property value by type](#format-property-value-by-type)\n      - [Number](#number)\n      - [Boolean](#boolean)\n    + [Encoding](#encoding)\n  * [Chaining Pattern](#chaining-pattern)\n- [Development](#development)\n- [CI CD github action](#ci-cd-github-action)\n- [License](#license)\n- [Buy me a Coffee](#buy-me-a-coffee)\n\n\u003c!-- tocstop --\u003e\n\n## Description\nConverts *csv* files to *JSON* files with Node.js. \n\nGive an input file like:\n\n|first_name|last_name|email|gender|age|zip|registered|\n|:----------:|:-------:|:---:|:----:|:---:|:---:|:---:|\n|Constantin|Langsdon|clangsdon0@hc360.com|Male|96|123|true|\n|Norah|Raison|nraison1@wired.com|Female|32| |false|\n\ne.g. :\n~~~\nfirst_name;last_name;email;gender;age;zip;registered\nConstantin;Langsdon;clangsdon0@hc360.com;Male;96;123;true\nNorah;Raison;nraison1@wired.com;Female;32;;false\n~~~\n\nwill generate:\n\n\n```json\n[\n {\n  \"first_name\": \"Constantin\",\n  \"last_name\": \"Langsdon\",\n  \"email\": \"clangsdon0@hc360.com\",\n  \"gender\": \"Male\",\n  \"age\": \"96\",\n  \"zip\": \"123\",\n  \"registered\": \"true\"\n },\n {\n  \"first_name\": \"Norah\",\n  \"last_name\": \"Raison\",\n  \"email\": \"nraison1@wired.com\",\n  \"gender\": \"Female\",\n  \"age\": \"32\",\n  \"zip\": \"\",\n  \"registered\": \"false\"\n }\n]\n```\n## Support for JS \u0026 TS\n\nThis package is compatible with ![JavaScript](https://img.shields.io/badge/javascript-%23323330.svg?style=for-the-badge\u0026logo=javascript\u0026logoColor=%23F7DF1E) and ![TypeScript](https://img.shields.io/badge/typescript-%23007ACC.svg?style=for-the-badge\u0026logo=typescript\u0026logoColor=white).\n\n## Prerequisites\n**NPM** (see [Installing Npm](https://docs.npmjs.com/getting-started/installing-node)).\n\n## Install npm *convert-csv-to-json package*\nGo to NPM package [convert-csv-to-json](https://www.npmjs.com/package/convert-csv-to-json).\n\n### Install\nInstall package in your *package.json*\n```bash\n$ npm install convert-csv-to-json --save\n```\nInstall package on your machine\n```bash\n$ npm install -g convert-csv-to-json\n```\n\n### Usage\n\n#### Generate JSON file\n```js\nlet csvToJson = require('convert-csv-to-json');\n\nlet fileInputName = 'myInputFile.csv'; \nlet fileOutputName = 'myOutputFile.json';\n\ncsvToJson.generateJsonFileFromCsv(fileInputName,fileOutputName);\n```\n#### Generate Array of Object in JSON format\n```js\nlet csvToJson = require('convert-csv-to-json');\n\nlet json = csvToJson.getJsonFromCsv(\"myInputFile.csv\");\nfor(let i=0; i\u003cjson.length;i++){\n    console.log(json[i]);\n}\n```\n\n#### Generate Object with sub array \n```\nfirstName;lastName;email;gender;age;birth;sons\nConstantin;Langsdon;clangsdon0@hc360.com;Male;96;10.02.1965;*diego,marek,dries*\n```\nGiven the above CSV example, to generate a JSON Object with properties that contains sub Array, like the property **sons** \nwith the values \u003cb\u003e*diego,marek,dries*\u003c/b\u003e you have to call the function ```parseSubArray(delimiter, separator)``` .\nTo generate the JSON Object with sub array from the above CSV example:\n```js\n    csvToJson.parseSubArray('*',',')\n                .getJsonFromCsv('myInputFile.csv');\n``` \nThe result will be:\n```json\n[\n  {\n      \"firstName\": \"Constantin\",\n      \"lastName\": \"Langsdon\",\n      \"email\": \"clangsdon0@hc360.com\",\n      \"gender\": \"Male\",\n      \"age\": \"96\",\n      \"birth\": \"10.02.1965\",\n      \"sons\": [\"diego\",\"marek\",\"dries\"]\n    }\n]\n``` \n\n#### Define field delimiter\nA field delimiter is needed to split the parsed values. As default the field delimiter is the **semicolon** (**;**), this means that during the parsing when a **semicolon (;)** is matched a new JSON entry is created.\nIn case your CSV file has defined another field delimiter you have to call the function ```fieldDelimiter(myDelimiter)``` and pass it as parameter the field delimiter.\n\nE.g. if your field delimiter is the comma **,** then:\n\n```js\n csvToJson.fieldDelimiter(',')\n            .getJsonFromCsv(fileInputName);\n```\n\n#### Support Quoted Fields\nTo be able to parse correctly fields wrapped in quote, like the **last_name** in the first row in the following example:\n\n|first_name|         last_name          |email|\n|:----------:|:--------------------------:|:---:|\n|Constantin| \"Langsdon,Nandson,Gangson\" |clangsdon0@hc360.com|\n\nyou need to activate the support quoted fields feature: \n\n```js\n csvToJson.supportQuotedField(true)\n            .getJsonFromCsv(fileInputName);\n```\n\nThe result will be:\n```json\n[\n  {\n      \"firstName\": \"Constantin\",\n      \"lastName\": \"Langsdon,Nandson,Gangson\",\n      \"email\": \"clangsdon0@hc360.com\"\n    }\n]\n``` \n\n#### Index header\nIf the header is not on the first line you can define the header index like: \n\n```js\n csvToJson.indexHeader(3)\n            .getJsonFromCsv(fileInputName);\n```\n\n#### Empty rows\nEmpty rows are ignored and not parsed.\n\n#### Format property value by type\nIf you want that a number will be printed as a Number type, and values *true* or *false* is printed as a boolean Type, use:\n```js\n csvToJson.formatValueByType()\n            .getJsonFromCsv(fileInputName);\n```\nFor example: \n\n```json\n[\n {\n  \"first_name\": \"Constantin\",\n  \"last_name\": \"Langsdon\",\n  \"email\": \"clangsdon0@hc360.com\",\n  \"gender\": \"Male\",\n  \"age\": 96,\n  \"zip\": 123,\n  \"registered\": true\n },\n {\n  \"first_name\": \"Norah\",\n  \"last_name\": \"Raison\",\n  \"email\": \"nraison1@wired.com\",\n  \"gender\": \"Female\",\n  \"age\": 32,\n  \"zip\": \"\",\n  \"registered\": false\n }\n]\n```\n##### Number\nThe property **age** is printed as \n```json\n \"age\": 32\n```\ninstead of\n```json\n  \"age\": \"32\"\n ```\n##### Boolean\nThe property **registered** is printed as \n```json\n \"registered\": true\n```\ninstead of\n```json\n  \"registered\": \"true\"\n ```\n\n#### Encoding\nYou can read and decode files with the following encoding:\n * utf8: \n    ```js\n     csvToJson.utf8Encoding()\n                .getJsonFromCsv(fileInputName);\n    ```\n * ucs2:\n    ```js\n      csvToJson.ucs2Encoding()\n                .getJsonFromCsv(fileInputName);\n     ```\n * utf16le:\n     ```js\n       csvToJson.utf16leEncoding()\n                  .getJsonFromCsv(fileInputName);\n      ```\n * latin1:\n     ```js\n       csvToJson.latin1Encoding()\n                  .getJsonFromCsv(fileInputName);\n      ```\n * ascii:\n     ```js\n       csvToJson.asciiEncoding()\n                  .getJsonFromCsv(fileInputName);\n      ```\n * base64:\n     ```js\n       csvToJson.base64Encoding()\n                  .getJsonFromCsv(fileInputName);\n      ```\n * hex:\n     ```js\n       csvToJson.hexEncoding()\n                  .getJsonFromCsv(fileInputName);\n      ```\n\n### Chaining Pattern\n\nThe 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.:\n\n```js\nlet csvToJson = require('convert-csv-to-json');\n\ncsvToJson.fieldDelimiter(',')\n            .formatValueByType()\n            .parseSubArray(\"*\",',')\n            .supportQuotedField(true)\n            .getJsonFromCsv('myInputFile.csv');\n\n```\n\n\n## Development\n* Download all csvToJson dependencies:\n    ~~~\n    npm install\n    ~~~\n* Run Tests\n    ~~~\n    npm test\n    ~~~\n* Debug Tests\n    ~~~\n    npm run test-debug\n    ~~~\n\n## CI CD github action\n\nThis repository uses the [GitHub Action iuccio/npm-semantic-publish-action@latest](https://github.com/marketplace/actions/npm-semver-publish-action) to publish the npm packeges.\nPushing on the master branch, depending on the git message, an new version will always be released.\nIf the commit message contains the keyword:\n* **[MAJOR]**: new major relase, e.g. v1.0.0 -\u003e v2.0.0 \n* **[PATCH]**: new patch relase, e.g. v1.0.0 -\u003e v1.0.1\n* without any of the above keywords a new minor relase will be applied, e.g. v1.0.0 -\u003e v1.1.0\n\n\n## License\nCSVtoJSON is licensed under the GNU General Public License v3.0 [License](LICENSE).\n\n## Buy me a Coffee\t\nJust if you want to support this repository:\t\n   * **BTC** tip address: \n37vdjQhbaR7k7XzhMKWzMcnqUxfw1njBNk","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiuccio%2FcsvToJson","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fiuccio%2FcsvToJson","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiuccio%2FcsvToJson/lists"}