Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 6143

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 60k
Author
Asked: November 27, 20242024-11-27T03:49:12+00:00 2024-11-27T03:49:12+00:00

How to download CSV and JSON files in React

  • 60k

This article was originally published on The Road To Enterprise blog. Read it there for the best reading experience.


There are websites that let users download CSV or JSON data as a file. This functionality can be quite useful, as users can download the data for further processing or to share it. In this article, you will learn how to add the functionality that will allow users to export a table in React and download it in JSON and CSV formats.

You can find the full code example in the GitHub repo.

Project Setup

First, let's create a new React project using Vite.

npm init vite@latest csv-json-files-download -- --template react 
Enter fullscreen mode Exit fullscreen mode

After the project is created, cd into it to install dependencies by running npm install and then start the dev server with npm run dev.

Next, we need to modify the App.jsx and App.css files.

src/App.jsx

import React from 'react'  function App() {   return (     <div className='App'>       <h1>How to download CSV and JSON files in React</h1>     </div>   ) }  export default App 
Enter fullscreen mode Exit fullscreen mode

src/App.css

.App {   max-width: 40rem;   margin: 2rem auto; } 
Enter fullscreen mode Exit fullscreen mode

That's enough for the initial setup. Let's start by adding functionality to export to JSON.

Export to JSON

Let's start by creating a file with users data that will be used for downloading a file and rendering a table.

src/users.json

{   "users": [     {       "id": 1,       "name": "Caitlyn",       "surname": "Kerluke",       "age": 24     },     {       "id": 2,       "name": "Rowan ",       "surname": "Nikolaus",       "age": 45     },     {       "id": 3,       "name": "Kassandra",       "surname": "Haley",       "age": 32     },     {       "id": 4,       "name": "Rusty",       "surname": "Arne",       "age": 58     }   ] } 
Enter fullscreen mode Exit fullscreen mode

Next, we need to update the App component to utilise the users data and display it in a table. Besides that, we will add a button to trigger the download. Below you can see the code for the App.jsx component. Besides the component. we have two functions: exportToJson and downloadFile. The former one calls the latter with appropriate arguments. The downloadFile function accepts an object as a parameter and expects three properties:

  • data
  • fileName
  • fileType

The data and fileType are used to create a blob that is downloaded. After that, we create an anchor element and dispatch a click event on it.

src/App.jsx

import React from 'react' import './App.css' import usersData from './users.json'  const downloadFile = ({ data, fileName, fileType }) => {   // Create a blob with the data we want to download as a file   const blob = new Blob([data], { type: fileType })   // Create an anchor element and dispatch a click event on it   // to trigger a download   const a = document.createElement('a')   a.download = fileName   a.href = window.URL.createObjectURL(blob)   const clickEvt = new MouseEvent('click', {     view: window,     bubbles: true,     cancelable: true,   })   a.dispatchEvent(clickEvt)   a.remove() }  const exportToJson = e => {   e.preventDefault()   downloadFile({     data: JSON.stringify(usersData.users),     fileName: 'users.json',     fileType: 'text/json',   }) }  function App() {   return (     <div className='App'>       <h1>How to download CSV and JSON files in React</h1>        <table className='usersTable'>         <thead>           <tr>             <th>ID</th>             <th>Name</th>             <th>Surname</th>             <th>Age</th>           </tr>         </thead>         <tbody>           {usersData.users.map(user => {             const { id, name, surname, age } = user             return (               <tr key={id}>                 <td>{id}</td>                 <td>{name}</td>                 <td>{surname}</td>                 <td>{age}</td>               </tr>             )           })}         </tbody>       </table>       <div className='actionBtns'>         <button type='button' onClick={exportToJson}>           Export to JSON         </button>       </div>     </div>   ) }  export default App 
Enter fullscreen mode Exit fullscreen mode

We can add a few styles, so the table looks a bit nicer.

src/App.css

.App {   max-width: 40rem;   margin: 2rem auto; }  .usersTable th, .usersTable td {   padding: 0.4rem 0.6rem;   text-align: left; }  .actionBtns {   margin-top: 1rem; }  .actionBtns button + button {   margin-left: 1rem; } 
Enter fullscreen mode Exit fullscreen mode

Great, now you should be able to download the users data as a JSON file by clicking on the Export to JSON button. Next, we will add Export to CSV functionality.

Export to CSV

We need another button that will be used to export data to a CSV file. Besides that, we also need a handler for it. The usersData is in the JSON format, so we will need to convert it to the CSV format, before passing it to the downloadFile function.

src/App.jsx

import React from 'react' import './App.css' import usersData from './users.json'  const downloadFile = ({ data, fileName, fileType }) => {   const blob = new Blob([data], { type: fileType })    const a = document.createElement('a')   a.download = fileName   a.href = window.URL.createObjectURL(blob)   const clickEvt = new MouseEvent('click', {     view: window,     bubbles: true,     cancelable: true,   })   a.dispatchEvent(clickEvt)   a.remove() }  const exportToJson = e => {   e.preventDefault()   downloadFile({     data: JSON.stringify(usersData.users),     fileName: 'users.json',     fileType: 'text/json',   }) }  const exportToCsv = e => {   e.preventDefault()    // Headers for each column   let headers = ['Id,Name,Surname,Age']    // Convert users data to a csv   let usersCsv = usersData.users.reduce((acc, user) => {     const { id, name, surname, age } = user     acc.push([id, name, surname, age].join(','))     return acc   }, [])    downloadFile({     data: [...headers, ...usersCsv].join('
'),     fileName: 'users.csv',     fileType: 'text/csv',   }) }  function App() {   return (     <div className='App'>       <h1>How to download CSV and JSON files in React</h1>        <table className='usersTable'>         <thead>           <tr>             <th>ID</th>             <th>Name</th>             <th>Surname</th>             <th>Age</th>           </tr>         </thead>         <tbody>           {usersData.users.map(user => {             const { id, name, surname, age } = user             return (               <tr key={id}>                 <td>{id}</td>                 <td>{name}</td>                 <td>{surname}</td>                 <td>{age}</td>               </tr>             )           })}         </tbody>       </table>       <div className='actionBtns'>         <button type='button' onClick={exportToJson}>           Export to JSON         </button>         <button type='button' onClick={exportToCsv}>           Export to CSV         </button>       </div>     </div>   ) }  export default App 
Enter fullscreen mode Exit fullscreen mode

Wrap up

There we have it. I hope you enjoyed this article. Now you should be well equipped with knowledge on how to add download files functionality to your own projects. Remember that even though I used React to demonstrate the download examples, you can use the download logic in other frameworks like Vue, Svelte, or Angular.


Want to stay up to date and learn more programming tips? Make sure to follow me on Twitter and subscribe to the newsletter!

javascriptjsonreactwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers
  • Author

    Refactoring for Efficiency: Tackling Performance Issues in Data-Heavy Pages

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.