Understanding JavaScript — Creating sample JSON data

shrey vijayvargiya
Apr 15 · 2 min read

Creating the sample JSON data of users

Design creative(Check more here)

Under the Hood

Link to JSON-Server: https://medium.com/p/4525def1aea5?source=post_stats_page-------------------------------------

While writing a blog about JSON npm, I was wondering that

Can I create my own JSON data by running a one JavaScript function?

So I start creating one, we need to use some JavaScript methods to create a sample JSON data.

Getting started

  • Math — This class has several important methods such as random and floor these methods create random fraction number and round them respectively.
  • Array- This constructor helps to construct an array object for you

The idea is to first create an array, followed by creating a single user and using for loop we will fill the array with a single user object and this will run until the length of our array. Every single user will have an id, name and age as key-value pair. An id is a number that is in increasing order, the name is a random string which we will generate using the random method and the age is again a number ranges from 1 to 100 so we will use random and floor method to get the random age.

See these small methods are sometimes essentials in terms of the interview, so I will recommend trying on your own.

Creating random users using a function

we have 3 functions here as follows-

  • getRandomAge — This function creates a random age using a random method and age ranges from 1–100
  • createUser — This function creates a single user by accepting id, and generating random age using getRandomAge method and random name.
  • createUsers — This function basically runs the for loop till the length of the array and create a new user every time using the createUser method and pushed that user in the user's array.

After running the code, we will get an array of 10 users with random name and age and increasing order id. In this way, we have created a sample JSON data of 10 users. We can make it up to 100 also by just increasing the length of our Array constructor.

Until, next time. Have a good day,People.

Nerd For Tech

From Confusion to Clarification

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/. Don’t forget to check out Ask-NFT, a mentorship ecosystem we’ve started

shrey vijayvargiya

Written by

Developer by profession, Designer by interests & Journalists by blood. I write about Tech, Design & Creative Affairs. Website — ihatereading.in

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/. Don’t forget to check out Ask-NFT, a mentorship ecosystem we’ve started

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store