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.
- 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.
- Node.js (Express.js)
- PostgreSQL
pg(PostgreSQL client for Node.js)dotenv(for environment variables)
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
- Clone the Repository
git clone https://github.com/yourusername/csv-to-json-api.git
cd csv-to-json-api
- Install Dependencies
npm install
- 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
);
- 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
- Run the Server
node index.js
Server will start at:
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%