A lightweight, dependency-minimal Node.js API for processing JSON datasets. This service allows users to perform filtering, sorting, and aggregation operations on datasets provided via HTTP POST requests.
- Data Filtering: Filter datasets based on specific key-value criteria.
- Data Sorting: Sort datasets by specific fields.
- Aggregation: Calculate Sum, Average, or Count for numeric fields.
- Input Validation: Robust JSON schema validation using
ajv. - Request Logging: Tracks request processing time and status codes.
-
Clone the repository
git clone <repository-url> cd json-api
-
Install dependencies
npm install
-
Start the server
npm test # OR node server.js
The server will start on
http://localhost:8000.
GET /
{
"message": "Server is running."
}POST /api/process
The API accepts a JSON body containing the dataset and the requested operation.
Filters the dataset where fields match the provided criteria.
Request:
{
"dataset": [
{ "id": 1, "role": "admin", "score": 10 },
{ "id": 2, "role": "user", "score": 20 },
{ "id": 3, "role": "admin", "score": 30 }
],
"operation": "filter",
"criteria": { "role": "admin" }
}Response:
{
"success": true,
"operation": "filter",
"result": [
{ "id": 1, "role": "admin", "score": 10 },
{ "id": 3, "role": "admin", "score": 30 }
]
}Sorts the dataset by the field specified in sortBy.
Request:
{
"dataset": [
{ "name": "Alice", "age": 30 },
{ "name": "Bob", "age": 25 }
],
"operation": "sort",
"sortBy": "age"
}Response:
{
"success": true,
"operation": "sort",
"result": [
{ "name": "Bob", "age": 25 },
{ "name": "Alice", "age": 30 }
]
}Performs calculations (sum, average, count) on a targetField.
Request:
{
"dataset": [
{ "item": "A", "price": 100 },
{ "item": "B", "price": 200 }
],
"operation": "aggregate",
"aggType": "average",
"targetField": "price"
}Response:
{
"success": true,
"operation": "aggregate",
"result": {
"average": 150
}
}json-api/
├── services/
│ └── operations.js # Core logic for filter, sort, aggregate
├── utils/
│ ├── bodyParser.js # Request body parsing helper
│ ├── logger.js # Request logging utility
│ └── validator.js # AJV schema validation
├── server.js # Main server entry point
├── package.json # Project dependencies
└── README.md # Project documentation
Requests are logged to server.log (if configured) or console, tracking the method, URL, and processing duration.
The API returns standard HTTP status codes:
200: Success400: Validation Error or Unknown Operation404: Endpoint Not Found500: Internal Server Error