Giter Site home page Giter Site logo

csv-converter's Introduction

CSV to JSON Converter

A quick and easy way to convert CSV files to JSON

Demo

Need a CSV file to test it out? Click Here

CSV To JSON Converter

Getting Started

Clone this repo and install dependencies with npm install. Create the build using npm run build and then run nodemon. Point your browser to http://localhost:5250 to see the app.

Editing the JSON Output

The default behavior is to transform each row of the CSV to the corresponding JSON. This output can be customized in server/index.js:

Turn Header Rows Off

To convert files that do not contain a header row, change csv({ noheader: false }) to csv({ noheader: true }). If you are further editing the behavior as described below, note that you will need to use field1, field2, etc to target particular rows.

Customize Output

Once the rows have been converted, they are passed to a .then() and can be customized here:

.then(lines => {
  for (let line of lines) {
    // Do something with each line
  }

For example, say you have a CSV file with location data that looks something like this, and want to group the JSON data by state, and also format the coordinates:

id city state lat lon
1 Anchorage AK 61.218 -149.900
2 Juneau AK 58.302 -134.420
3 Birmingham AL 33.521 -86.802
4 Decatur AL 34.606 -86.983
5 Mobile AL 30.694 -88.043
6 Jacksonville AR 34.866 -92.110
7 Little Rock AR 34.746 -92.290
8 Texarkana AR 33.442 -94.038
const data = {};
for (let line of lines) {
  if (!data.hasOwnProperty(line.state)) {
    data[line.state] = {};
  }
  data[line.state][line.city] = {
    coords: `${line.lat}, ${line.lon}`,
  };
}

** Note: ** If you are not using a header row, line.state becomes line.field3, line.city becomes line.field4, etc.

To write the correct data to the file and get the right preview information, we'll also update the write command and response to use the data object we just created:

fs.writeFile(`public/downloads/${filename}.json`, JSON.stringify(data, null, 2), function(error) {
  if (error) {
    res.status(422).json({ error });
  }
  res.status(200).json({ filename, preview: data[Object.keys(data)[0]] });
});

The resulting file should look like this:

{
  "AK": {
    "Anchorage": {
      "coords": "61.218, -149.9"
    },
    "Juneau": {
      "coords": "58.302, -134.42"
    }
  },
  "AL": {
    "Birmingham": {
      "coords": "33.521, -86.802"
    },
    "Decatur": {
      "coords": "34.606, -86.983"
    },
    "Mobile": {
      "coords": "30.694, -88.043"
    }
  },
  "AR": {
    "Jacksonville": {
      "coords": "34.866, -92.11"
    },
    "Little Rock": {
      "coords": "34.746, -92.29"
    },
    "Texarkana": {
      "coords": "33.442, -94.038"
    }
  }
}

Now imagine we want to use this as a sort of JSON location map, so we'll need to make some more adjustments. Let's make all the properties lowercase and get rid of the spaces. To retain the proper capitalization and formatting, we'll also add a formatted location field:

const data = {};
for (let line of lines) {
  if (!data.hasOwnProperty(line.state.toLowerCase())) {
    data[line.state.toLowerCase()] = {};
  }
  data[line.state.toLowerCase()][
    line.city
      .toLowerCase()
      .split(' ')
      .join('_')
  ] = {
    coords: `${line.lat}, ${line.lon}`,
    formatted_location: `${line.city}, ${line.state}`,
  };
}

Our output now has everything we need:

{
  "ak": {
    "anchorage": {
      "coords": "61.218, -149.9",
      "formatted_location": "Anchorage, AK"
    },
    "juneau": {
      "coords": "58.302, -134.42",
      "formatted_location": "Juneau, AK"
    }
  },
  "al": {
    "birmingham": {
      "coords": "33.521, -86.802",
      "formatted_location": "Birmingham, AL"
    },
    "decatur": {
      "coords": "34.606, -86.983",
      "formatted_location": "Decatur, AL"
    },
    "mobile": {
      "coords": "30.694, -88.043",
      "formatted_location": "Mobile, AL"
    }
  },
  "ar": {
    "jacksonville": {
      "coords": "34.866, -92.11",
      "formatted_location": "Jacksonville, AR"
    },
    "little_rock": {
      "coords": "34.746, -92.29",
      "formatted_location": "Little Rock, AR"
    },
    "texarkana": {
      "coords": "33.442, -94.038",
      "formatted_location": "Texarkana, AR"
    }
  }
}

Clearing Old Files

Files are cleared automatically by a cron job that runs nightly at midnight. This can be adjusted if needed in server/index.js.

Built With

License

This project is licensed under the MIT License - see the LICENSE.md file for details

csv-converter's People

Contributors

andreareed avatar

Watchers

James Cloos avatar  avatar

Forkers

mumer29

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.