DEV Community

Sanchit Gupta
Sanchit Gupta

Posted on

1 1

Snippet... Working with CSV in Javascript with PapaParse

https://www.apps4developers.com/csv/

Installing papaparse

Install PapaParse library from npm.

npm install papaparse
Enter fullscreen mode Exit fullscreen mode

Learn more about PapaParse here.

CSV to JSON With Header

import Papa from 'papaparse';

const csvString = `Column 1,Column 2,Column 3,Column 4
1-1,1-2,1-3,1-4
2-1,2-2,2-3,2-4
3-1,3-2,3-3,3-4
4,5,6,7`;
const jsonObj = Papa.parse(csvString, { header: true })

console.log(jsonObj);
Enter fullscreen mode Exit fullscreen mode

CSV to JSON Without Header

import Papa from 'papaparse';

const csvString = `1-1,1-2,1-3,1-4
2-1,2-2,2-3,2-4
3-1,3-2,3-3,3-4
4,5,6,7`;
const jsonObj = Papa.parse(csvString, { header: false })

console.log(jsonObj);
Enter fullscreen mode Exit fullscreen mode

Convert JSON to CSV

import Papa from 'papaparse';

const jsonObj = [
    {
        "Column 1": "1-1",
        "Column 2": "1-2",
        "Column 3": "1-3",
        "Column 4": "1-4"
    },
    {
        "Column 1": "2-1",
        "Column 2": "2-2",
        "Column 3": "2-3",
        "Column 4": "2-4"
    },
    {
        "Column 1": "3-1",
        "Column 2": "3-2",
        "Column 3": "3-3",
        "Column 4": "3-4"
    },
    {
        "Column 1": 4,
        "Column 2": 5,
        "Column 3": 6,
        "Column 4": 7
    }
];
const csvString = Papa.unparse(jsonObj, { header: false })

console.log(csvString);
Enter fullscreen mode Exit fullscreen mode

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay