randomjsongen v1.0.2
randomjsongen - Random JSON Data Generator
npm install randomjsongen const RJSON = require('randomjsongen');
Table of Contents
Installation
To install this package, use npm:
npm install randomjsongen
Usage
To use randomjsongen
, require the package and call it with the desired fields and the number of records you want to generate. Here's an example of basic usage:
Example 1: Generate 5 Random Records with Predefined Fields
const RJSON = require('randomjsongen');
// Generate 5 random records with predefined fields
const data = RJSON([
{ name: { type: 'name' }, email: { type: 'email' }, phone: { type: 'phone' } }
], 5);
console.log(data);
Example 2: Generate Custom Records with Defined Field Types
const RJSON = require('randomjsongen');
// Generate 3 records with custom fields and types
const data = RJSON([
{ customField1: { type: 'string', length: 8 }, customField2: { type: 'boolean' } }
], 3);
console.log(data);
Example with Custom Fields
You can define custom fields with types, such as string
, number
, boolean
, etc. You can also specify additional options, such as the length of the string or the range for numbers.
Example 3: Custom String and Number Fields
const RJSON = require('randomjsongen');
// Generate 2 records with custom fields and options
const data = RJSON([
{ username: { type: 'string', length: 10 }, age: { type: 'number', min: 18, max: 50 } }
], 2);
console.log(data);
Fields & Options
Predefined Fields
Here are the available predefined fields and what they generate:
- name: A random full name (e.g., "John Doe")
- email: A random email based on the name (e.g., "johndoe@email.com")
- phone: A random 10-digit phone number
- dob: A random date of birth in
dd Month yyyy
format (e.g., "31 Dec 1990") - company: A random company name (e.g., "Tech Solutions Inc.")
- jobTitle: A random job title (e.g., "Software Engineer")
- website: A random website URL (e.g., "https://johndoe.com")
- address: A random street address (e.g., "123 Main St.")
- city: A random city name (e.g., "New York")
- state: A random state name (e.g., "California")
- country: A random country name (e.g., "United States")
- zip: A random zip code (e.g., "12345")
- isAdmin: A random boolean value (
true
orfalse
) - status: A random boolean value (
true
orfalse
) - lastLogin: A random last login timestamp
- age: A random age (based on
dob
)
Custom Fields
For custom fields, you can define a type and additional options such as length
, min
, and max
.
- String: Generates random strings. You can specify the
length
option to define the length of the string. - Number: Generates random numbers. You can define
min
andmax
for the range of numbers. - Boolean: Generates random boolean values (
true
orfalse
). - Date: Generates random dates. Optionally, you can specify a range of dates.
Example for Generating a Random String:
{ fieldName: { type: 'string', length: 10 } }
Sample Output
Here’s an example of the output you can expect from randomjsongen
:
[
{
"_id": "f21b7267bc83517b3075fc63",
"name": "Dallas Gottlieb",
"email": "dallasgottlieb@email.com",
"phone": "2360123195",
"address": "520 Rippin Loop",
"city": "Monicaview",
"state": "Mississippi",
"country": "Philippines",
"zip": "86437",
"dob": "10 Sep 2012",
"company": "Sauer, Purdy and Welch",
"jobTitle": "Future Group Director",
"website": "https://alessandro.info",
"isAdmin": true,
"status": true,
"lastLogin": "2024-02-20T03:43:49.842Z",
"age": 22,
"customField": "f6knkr"
},
{
"_id": "f21b7267bc83517b3075fc64",
"name": "Jane Doe",
"email": "janedoe@email.com",
"phone": "2360123196",
"address": "700 Birchwood Dr",
"city": "Lakeside",
"state": "California",
"country": "United States",
"zip": "12345",
"dob": "5 May 1992",
"company": "Global Tech Corp.",
"jobTitle": "Product Manager",
"website": "https://janedoe.com",
"isAdmin": false,
"status": true,
"lastLogin": "2024-02-18T05:11:12.349Z",
"age": 32,
"customField": "r8j2m7"
}
]
Contributing
Contributions are welcome!
If you find a bug or have suggestions, please open an issue or a pull request on GitHub.