How to Convert CSV to JSON in ReactJS ?
Last Updated :
18 Apr, 2024
Dealing with data in various formats is a common task in web development. CSV (Comma Separated Values) and JSON
(JavaScript Object Notation) are two widely used formats for storing and transmitting data. Converting CSV to JSON is often required when working with data in ReactJS applications.
Approach
- Begin by creating a new React application using the Create React App or any other method you prefer. This will give you the basic structure to work with.
- You'll need to import the necessary dependencies. In this case, we're using
React
for building the UI, useState
hook to manage state, and PapaParse
for parsing CSV files. - Build the UI components to display the CSV data and the converted JSON data. Use conditional rendering to display the data only when it's available. Display the CSV data in a table format and the JSON data in a preformatted text.
- Create a function to handle CSV file uploads. This function will be triggered when a user selects a CSV file. Check if the file is a CSV file and then use
PapaParse
to parse the CSV data. - Write a function to convert the parsed CSV data into JSON format. You can use
JSON.stringify()
to convert the data.
Steps to Create a React Application
Step 1: Create a new React project with the following command.
npx create-react-app myproject
Step 2: Switch to the Project Directory
cd myproject
Step 3: Once you have your project set up, run the following command to install the papa parse package.
npm i papaparse
Project structure

The updated dependencies in package.json file will look like:
{
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"papaparse": "^5.4.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router": "^6.17.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
}
Example: In the below example, the Papaparse library is used to parse a CSV file uploaded by the user and convert it into JSON format, with proper file type validation and display of CSV and JSON data in a React.js application.
CSS
/*App.css*/
.container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
background-color: #a8ffb6;
border-radius: 8px;
}
.header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 20px;
}
.content {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 20px;
}
.data-container {
background-color: #fff;
padding: 15px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.button {
padding: 10px 20px;
background-color: #fb0000;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s ease;
}
table {
width: 100%;
border-collapse: collapse;
}
th,
td {
border: 1px solid rgb(255, 0, 0);
padding: 8px;
}
pre {
white-space: pre-wrap;
}
JavaScript
//App.js
import React, { useState } from 'react';
import Papa from 'papaparse';
import './App.css';
function App() {
const [csvData, setCsvData] = useState([]);
const [jsonData, setJsonData] = useState([]);
const csvUploadFn = (event) => {
const file = event.target.files[0];
const fileType =
file.name.split('.').pop().toLowerCase();
if (fileType !== 'csv') {
alert('Please upload a CSV file.');
return;
}
Papa.parse(file, {
complete: (result) => {
setCsvData(result.data);
},
header: true,
});
};
const conversionFn = () => {
const res = JSON.stringify(csvData, null, 2);
setJsonData(res);
};
return (
<div className="container">
<div className="header">
<h1>CSV to JSON Converter</h1>
<input type="file"
onChange={csvUploadFn}
accept=".csv" />
<button className="button"
onClick={conversionFn}>
Convert to JSON
</button>
</div>
<div className="content">
{csvData.length > 0 && (
<div className="data-container">
<h2>CSV Data</h2>
<table>
<thead>
<tr>
{Object.keys(csvData[0]).map(
(header, index) => (
<th key={index}>
{header}
</th>
))}
</tr>
</thead>
<tbody>
{csvData.map((row, rowIndex) => (
<tr key={rowIndex}>
{Object.values(row).map(
(cell, cellIndex) => (
<td key={cellIndex}>
{cell}
</td>
))}
</tr>
))}
</tbody>
</table>
</div>
)}
{jsonData && (
<div className="data-container">
<h2>JSON Data</h2>
<pre>{jsonData}</pre>
</div>
)}
</div>
</div>
);
}
export default App;
Step to run the application: Open the terminal and type the following command.
npm start
Output:Â This output will be visible on http://localhost:3000/ on browser window.

Similar Reads
How to Convert CSV to JSON in JavaScript ? In this article, we will explain different ways to change Comma-Separated Values (CSV) data into JavaScript Object Notation (JSON) format, step-by-step. We'll break down each method with clear explanations and examples. There are several approaches available in JavaScript to convert CSV to JSON in J
3 min read
How to Convert JSON Object to CSV in JavaScript ? JSON (JavaScript Object Notation) and CSV (Comma-Separated Values) are two widely used formats, each with its own strengths and applications. Fortunately, JavaScript provides powerful tools to facilitate the conversion process between these formats. These are the following approaches: Table of Conte
3 min read
How to Convert CSV to Excel in Node.js ? NodeJS has become one of the famous backend frameworks for development. So in this article, we'll see one of its use to convert CSV into Excel We will use CSVtoExcel npm package to do the conversion of files. It provides convertCsvToXlsx function to implement the conversion. convertCsvToXlsx(source,
2 min read
Convert nested JSON to CSV in Python In this article, we will discuss how can we convert nested JSON to CSV in Python. An example of a simple JSON file: A simple JSON representationAs you can see in the example, a single key-value pair is separated by a colon (:) whereas each key-value pairs are separated by a comma (,). Here, "name",
9 min read
Convert JSON to CSV in Python We can convert JSON to CSV in Python using the built-in json and csv modules.What is JSON and CSV?JSON is a lightweight, text-based data format commonly used to exchange data between web servers and clients. It stores data in key-value pairs and is typically used for data transmission in web applica
2 min read
How to Convert JSON to Excel in JavaScript? It is often necessary to export or download JSON data in the form of Excel spreadsheets when developing web applications, any web developer would be able to go through this article as it provides a useful function of converting JSON files to Excel format using SheetsJS through JavaScript.These are t
4 min read
How to Convert Excel to JSON in JavaScript ? Converting Excel spreadsheets to JSON format is a common requirement in various applications. JavaScript code utilizes the read-excel-file library to parse the Excel data, convert it to JSON format, and display it. Additionally, it provides functionality to download the generated JSON file. Approach
3 min read
How to write ReactJS Code in Codepen.IO ? Now everything is online, some people use VScode to write react.js code and face most of the difficulty. The VScode requires setting for writing React.js code and Many beginners faced difficulty to use VScode so, for them, it is good and easy to use codepen. The codepen provide you with an online pl
2 min read
How to use List Component in ReactJS? Lists are continuous, vertical indexes of text or images. Material UI for React has this component available for us, and it is very easy to integrate. We can use the List Component in ReactJS using the following approach. Prerequisites:NodeJS or NPMReactJSSteps to Create the React Application And In
2 min read
How to Convert JSON file into CSV in PHP ? In this article, we are going to see how to convert JSON data into a CSV file using PHP. JSON (JavaScript Object Notation) is a dictionary-like notation that can be used to structuring data. It is stored with the extension .json, for example - geeksforgeeks.json On the other hand, CSV (or Comma Sepa
2 min read