Random JSON Generator Script

JS
S
JavaScript

Generate samples of JSON

1const fs = require('fs');
2
3const getRandomString = size => {
4  return Math.random()
5    .toString(36)
6    .substring(size);
7};
8
9const generateRandomNumber = (min, max) => {
10  min = Math.ceil(min);
11  max = Math.floor(max);
12  return Math.floor(Math.random() * (max - min)) + min;
13};
14
15// Tasks
16const taskTypes = ['VIP_SUPPORT', 'CHECK_IN', 'CHECK_OUT', 'TENTS', 'NURSING', 'MEDICAL', 'TECH_ADVISE', 'TECH_SUPPORT'];
17const timeSlots = [
18  {
19    startsAt: '08:00',
20    endsAt: '10:00'
21  },
22  {
23    startsAt: '10:00',
24    endsAt: '12:00'
25  },
26  {
27    startsAt: '12:00',
28    endsAt: '16:00'
29  },
30  {
31    startsAt: '08:00',
32    endsAt: '12:00'
33  },
34  {
35    startsAt: '16:00',
36    endsAt: '18:00'
37  }
38];
39const taskBlueprint = {
40  id: 'ws2020324',
41  name: 'VIP Support Sector A',
42  type: 'VIP_SUPPORT',
43  area: 'A16',
44  startsAt: '08:00',
45  endsAt: '12:00',
46  startedAt: '',
47  endedAt: ''
48};
49
50// Volunteers
51const allVolunteers = [];
52const blueprint = {
53  id: 'vol23458',
54  name: 'John',
55  email: 'john@gmail.com',
56  areas: ['A16', 'A1'],
57  groups: ['G12', 'G1'],
58  tasks: []
59};
60// for (let index = 0; index < 500; index++) {
61//   const newVolunteer = { ...blueprint };
62//   newVolunteer.id = `vol001${generateRandomNumber(0, 500)}`;
63//   newVolunteer.name = `John ${getRandomString()}`;
64//   newVolunteer.email = `${newVolunteer.name}@mail.com`;
65//   const areas = [];
66//   for (let ai = 0; ai < 10; ai++) {
67//     areas.push(`A${generateRandomNumber(0, 10)}`);
68//   }
69//   newVolunteer.areas = areas;
70//   const groups = [];
71//   for (let ai = 0; ai < 12; ai++) {
72//     groups.push(`G${generateRandomNumber(0, 12)}`);
73//   }
74//   newVolunteer.groups = groups;
75
76//   const tasks = [];
77//   const maxTasks = generateRandomNumber(2, 6);
78//   for (let ai = 0; ai < maxTasks; ai++) {
79//     const newTask = { ...taskBlueprint };
80//     newTask.type = taskTypes[generateRandomNumber(0, taskTypes.length - 1)];
81//     newTask.name = `${newTask.type} ${getRandomString()}`;
82//     const timeSlot = timeSlots[generateRandomNumber(0, timeSlots.length - 1)];
83//     newTask.startsAt = timeSlot.startsAt;
84//     newTask.endsAt = timeSlot.endsAt;
85//     newTask.area = `A${generateRandomNumber(0, 10)}`;
86//     tasks.push(newTask);
87//   }
88//   newVolunteer.tasks = tasks;
89//   allVolunteers.push(newVolunteer);
90// }
91
92const allTasks = [];
93for (let ai = 0; ai < 30; ai++) {
94  const newTask = { ...taskBlueprint };
95  newTask.type = taskTypes[generateRandomNumber(0, taskTypes.length - 1)];
96  newTask.name = `${newTask.type} ${getRandomString()}`;
97  const timeSlot = timeSlots[generateRandomNumber(0, timeSlots.length - 1)];
98  newTask.startsAt = timeSlot.startsAt;
99  newTask.endsAt = timeSlot.endsAt;
100  newTask.area = `A${generateRandomNumber(0, 10)}`;
101  allTasks.push(newTask);
102}
103
104// Write to file
105let data = JSON.stringify(allTasks);
106fs.writeFileSync('unassigned_tasks.json', data);
107

Created on 2/9/2020