{"id":25799653,"url":"https://github.com/filipdanic/json2csvexporter","last_synced_at":"2025-02-27T15:47:33.210Z","repository":{"id":10248879,"uuid":"65096944","full_name":"filipdanic/json2csvexporter","owner":"filipdanic","description":"A simple little library for exporting simple JSON structures to a CSV file. ","archived":false,"fork":false,"pushed_at":"2023-01-05T18:34:58.000Z","size":306,"stargazers_count":5,"open_issues_count":6,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-03T12:06:02.674Z","etag":null,"topics":["csv","exporter","json"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/filipdanic.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-08-06T18:44:07.000Z","updated_at":"2022-05-01T10:59:50.000Z","dependencies_parsed_at":"2023-01-13T15:49:47.912Z","dependency_job_id":null,"html_url":"https://github.com/filipdanic/json2csvexporter","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/filipdanic%2Fjson2csvexporter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/filipdanic%2Fjson2csvexporter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/filipdanic%2Fjson2csvexporter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/filipdanic%2Fjson2csvexporter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/filipdanic","download_url":"https://codeload.github.com/filipdanic/json2csvexporter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241030646,"owners_count":19897135,"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","exporter","json"],"created_at":"2025-02-27T15:47:32.300Z","updated_at":"2025-02-27T15:47:33.202Z","avatar_url":"https://github.com/filipdanic.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# json2csvexporter\n\nThis is a simple little library for exporting simple JSON structures to a CSV file. The download will trigger the user to save the file to their system. The library itself does not handle any File I/O.\n\nCurrently, json2csvexporter does not support nested JSON data. As such, you will probably need to reduce complex structures into a simple `key: value` schema.\n\nTo install:\n\n`npm i json2csvexporter --save`\n\nProvided are:\n* umd-formatted file: dist/CSVExportService.js (browser-ready)\n* commonjs-formatted file: lib/CSVExportService.js\n* es-module: module/CSVExportService.js\n\nTable of contents:\n\n1. [Simple Example](#simple-example)\n2. [Example with Custom Options](#example-with-custom-options)\n3. [Example with TSV and Formatters](#example-with-tsv-and-formatters)\n4. [JSON to CSV/TSV encoded String/Blob (without downloading on the client)](#json-to-csvtsv-encoded-stringblob-without-downloading-on-the-client)\n5. [API and Options Docs](#api-and-options-docs)\n6. [Browsers Support](#browsers-support)\n7. [Contributing](#contributing)\n\n## Simple Example\n\nIn this simple example we take a JSON object and print all of the properties in it to the CSV. There is also [an online demo available via CodeSandbox.](https://codesandbox.io/s/json2csvexporter-e8xbl)\n\n```javascript\nimport CSVExportService from 'json2csvexporter';\n...\nconst vehiclesJSON = [\n  {id: 1, make: 'Toyota', model: 'Corolla', year: 2014},\n  {id: 2, make: 'Ford', model: 'Mustang', year: 2012},\n  {id: 3, make: 'Toyota', model: '', year: ''}\n];\nconst exporter = CSVExportService.create();\nexporter.downloadCSV(vehiclesJSON);\n```\n\nThis will generate the following CSV:\n\n```csv\nid,make,model,year\n1,Toyota,Corolla,2014\n2,Ford,Mustang,2012\n3,Toyota,,\n```\n\n## Example with Custom Options\n\nIn this example, we will take the same JSON object from the previous example, but only print a list of properties that we want. We will also decorate the headers with different names.\n\n```javascript\nimport CSVExportService from 'json2csvexporter';\n...\nconst csvColumnsList = ['id', 'make', 'model', 'year'];\nconst csvColumnsMap = {\n  id: 'ID',\n  make: 'Make',\n  model: 'Model Name',\n  year: 'Year'\n};\nconst vehiclesJSON = [\n  {id: 1, make: 'Toyota', model: 'Corolla', year: 2014},\n  {id: 2, make: 'Ford', model: 'Mustang', year: 2012},\n  {id: 3, make: 'Toyota', model: '', year: ''}\n];\nconst exporter = CSVExportService.create({\n  columns: csvColumnsList,\n  headers: csvColumnsMap,\n  includeHeaders: true,\n});\nexporter.downloadCSV(vehiclesJSON);\n```\nThis will generate the following CSV:\n\n```csv\nID,Make,Model Name,Year\n1,Toyota,Corolla,2014\n2,Ford,Mustang,2012\n3,Toyota,,\n```\n\n## Example with TSV and Formatters\n\nIn this example, we will make a TSV file and create formatting functions to style the content of certain properties.\n\n```javascript\nimport CSVExportService from 'json2csvexporter';\n...\n// using the same vehiclesJSON as before...\n\nconst exporter = CSVExportService.create({\n  contentType: 'text/tsv',\n  filename: 'newformat.tsv',\n  delimiter: '\\t',\n  formatters: {\n    id: (id) =\u003e {\n      return `#${id}`;\n    },\n    model: (model) =\u003e {\n      return model || 'Unknown model';\n    },\n    year: (year) =\u003e {\n      return year || 'Unknown year';\n    }\n  }\n});\nexporter.downloadCSV(vehiclesJSON);\n\n```\n\nThis will generate the following TSV:\n\n```tsv\nid  make  model year\n#1  Toyota  Corolla 2014\n#2  Ford  Mustang 2012\n#3  Toyota  Unknown model  Unknown year\n```\n\n## JSON to CSV/TSV encoded String/Blob (without downloading on the client)\n\n```javascript\nimport CSVExportService from 'json2csvexporter';\n...\nconst vehiclesJSON = [\n  {id: 1, make: 'Toyota', model: 'Corolla', year: 2014},\n  {id: 2, make: 'Ford', model: 'Mustang', year: 2012},\n  {id: 3, make: 'Toyota', model: '', year: ''}\n];\nconst exporter = CSVExportService.create();\nexporter.dataToString(vehiclesJSON);\n```\n\nThis will return the CSV as a JavaScript String. This allows you to further manipulate the data, save to a file, a database or anything else.\n\nSimilarly, calling:\n\n```javascript\nexporter.createCSVBlob(vehiclesJSON);\n```\n\nWill return a `Blob {size: 76, type: \"text/csv\"}`, or in other words, a regular JavaScript blob object that is encoded as the type you specified (in this case a plain text/csv). You then manipulate this Blob object however you like.\n\n\n## API and Options Docs\n\n### Options\n\nWhen creating a new `CSVExportService` you can pass in an options objects. These are the options that you can use:\n\n- `columns`: an Array of Strings. Each element represents a property of the JSON object that should be extracted. If it is not provided, then the exporter will iterate through all of the properties.\n\n- `contentType`: the content type of the file. Default is `text/csv`\n\n- `delimiter`: the delimiter to separate values. The default is a comma.\n\n- `formatters`: an Object. Each property defines how to format a value corresponding to the same key.\n\n- `headers`: an Object. Works as a map to stylize keys in the header. E.g. `{firstName: 'First Name'}`\n\n- `includeHeaders`: a Boolean. Defaults to true.\n\n- `devMode`: a Boolean. Useful for debugging. Defaults to false.\n\n### API\n\nThe `CSVExportService` implements two static methods: `create(options)` which returns a new instance of CSVExportService and `download(data, options)` which initializes a new service and downloads the data right away.\n\nFor more info, dive into the code. It is a very simple class.\n\n## Browsers Support\n\n| [\u003cimg src=\"https://raw.githubusercontent.com/godban/browsers-support-badges/master/src/images/edge.png\" alt=\"IE / Edge\" width=\"16px\" height=\"16px\" /\u003e](http://godban.github.io/browsers-support-badges/)\u003c/br\u003eIE / Edge | [\u003cimg src=\"https://raw.githubusercontent.com/godban/browsers-support-badges/master/src/images/firefox.png\" alt=\"Firefox\" width=\"16px\" height=\"16px\" /\u003e](http://godban.github.io/browsers-support-badges/)\u003c/br\u003eFirefox | [\u003cimg src=\"https://raw.githubusercontent.com/godban/browsers-support-badges/master/src/images/chrome.png\" alt=\"Chrome\" width=\"16px\" height=\"16px\" /\u003e](http://godban.github.io/browsers-support-badges/)\u003c/br\u003eChrome | [\u003cimg src=\"https://raw.githubusercontent.com/godban/browsers-support-badges/master/src/images/safari.png\" alt=\"Safari\" width=\"16px\" height=\"16px\" /\u003e](http://godban.github.io/browsers-support-badges/)\u003c/br\u003eSafari | [\u003cimg src=\"https://raw.githubusercontent.com/godban/browsers-support-badges/master/src/images/opera.png\" alt=\"Opera\" width=\"16px\" height=\"16px\" /\u003e](http://godban.github.io/browsers-support-badges/)\u003c/br\u003eOpera | [\u003cimg src=\"https://raw.githubusercontent.com/godban/browsers-support-badges/master/src/images/safari-ios.png\" alt=\"iOS Safari\" width=\"16px\" height=\"16px\" /\u003e](http://godban.github.io/browsers-support-badges/)\u003c/br\u003eiOS Safari |\n| --------- | --------- | --------- | --------- | --------- | --------- |\n| IE10, IE11, Edge| 4+ | 13+ | 5+ | 12+ | 7+ |\n\n\n## Contributing\n\n1. Submit issues or suggest features.\n2. Send PRs with bug fixes, tests, new methods, better docs etc.\n3. Share the love!\n\n## Contributor list\n\n1. [@filipdanic](https://github.com/filipdanic)\n2. [@androidfanboi](https://github.com/androidfanboi)\n3. [@erickzhao](https://github.com/erickzhao)\n4. [@bira91](https://github.com/bira91)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffilipdanic%2Fjson2csvexporter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffilipdanic%2Fjson2csvexporter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffilipdanic%2Fjson2csvexporter/lists"}