App.jsx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
// React.js Project to Convert JSON to CSV Using react-papaparse Library import React, { useState } from 'react'; import { usePapaParse } from 'react-papaparse'; import { Form, Button, Container, Row, Col, Alert } from 'react-bootstrap'; import "bootstrap/dist/css/bootstrap.min.css" export default function JsonToCSV() { const { jsonToCSV } = usePapaParse(); const [jsonInput, setJsonInput] = useState(''); const [error, setError] = useState(''); const handleJsonToCSV = () => { try { const jsonData = JSON.parse(jsonInput); const results = jsonToCSV(jsonData); // Create a Blob and download it const blob = new Blob([results], { type: 'text/csv;charset=utf-8;' }); const link = document.createElement('a'); link.href = URL.createObjectURL(blob); link.download = 'converted.csv'; link.click(); // Clear errors setError(''); } catch (err) { setError('Invalid JSON input. Please check your data.'); } }; return ( <Container className="mt-4"> <h1 className="text-center">JSON to CSV Converter</h1> <Row className="justify-content-center"> <Col md={8}> <Form> <Form.Group controlId="jsonInput"> <Form.Label>Paste your JSON here</Form.Label> <Form.Control as="textarea" rows={8} placeholder="Enter raw JSON data..." value={jsonInput} onChange={(e) => setJsonInput(e.target.value)} /> </Form.Group> {error && <Alert variant="danger" className="mt-3">{error}</Alert>} <Button variant="primary" className="mt-3" onClick={handleJsonToCSV} > Convert to CSV and Download </Button> </Form> </Col> </Row> </Container> ); } |