Skip to content

AthrvaNaik/CSV_to_JSON-API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CSV to JSON API

A Node.js API that reads a CSV file, converts each row into JSON, and uploads the data into a PostgreSQL database. The API also calculates and displays age group distribution.


Features

  • Parses CSV files without using third-party CSV libraries.
  • Handles complex/nested properties in CSV (dot notation, e.g., address.city).
  • Stores mandatory fields (name, age) in PostgreSQL columns.
  • Stores additional fields in a JSONB column (additional_info).
  • Calculates age distribution of all users and prints percentages in console.
  • Configurable database connection using .env.

Technology Stack

  • Node.js (Express.js)
  • PostgreSQL
  • pg (PostgreSQL client for Node.js)
  • dotenv (for environment variables)

Project Structure

csv-to-json-api/
│
├── routes/
│   └── upload.js       # API endpoint for CSV upload
├── db.js               # Database connection & insert logic
├── index.js            # Main server file
├── .env                # Environment variables (not included in repo)
├── package.json
├── package-lock.json
└── README.md

Setup Instructions

  1. Clone the Repository
git clone https://github.com/yourusername/csv-to-json-api.git
cd csv-to-json-api
  1. Install Dependencies
npm install
  1. Create PostgreSQL Database

Open pgAdmin or psql.

Create a new database and user:

CREATE DATABASE my_app_db;

CREATE ROLE my_app_user WITH LOGIN PASSWORD 'password123';
GRANT ALL PRIVILEGES ON DATABASE my_app_db TO my_app_user;

Create users table:

CREATE TABLE public.users (
    id SERIAL PRIMARY KEY,
    name VARCHAR NOT NULL,
    age INT NOT NULL,
    address JSONB,
    additional_info JSONB
);
  1. Configure .env File

Create a .env file in the root folder:

PGHOST=127.0.0.1
PGUSER=my_app_user
PGPASSWORD=password123
PGDATABASE=my_app_db
PGPORT=5432
PORT=3000
  1. Run the Server
node index.js

Server will start at:

http://localhost:3000

API Usage POST /api/upload

Description: Upload a CSV file and insert the data into the database.

Request Body: Form-data with key file and CSV file as value.

Response Example:

{
    "message": "CSV uploaded and processed successfully!"
}

CSV Format Example name.firstName,name.lastName,age,address.line1,address.line2,address.city,address.state,gender Rohit,Prasad,35,A-563 Rakshak Society,New Pune Road,Pune,Maharashtra,male Riya,Shah,22,Sunshine Apartments,MG Road,Mumbai,Maharashtra,female

Mandatory fields: name.firstName, name.lastName, age

Optional/extra fields are stored in additional_info.

Console Output

After processing, the server prints age distribution:

Age-Group % Distribution
<20 : 25.00%
20-40 : 50.00%
40-60 : 25.00%
>60 : 0.00%

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published