4.2: Generating Seed Data

We need some Users to begin adding more features.

We could manually register users, enter their profile information, find photos for them, etc.

But, we're programmers. We're lazy.

We can do it with less effort.

json-generator.com

One option for generating seed data is www.json-generator.com. It's the method we'll be using.

There are many other options like the npm Faker library or the NuGet package. Feel free to experiment with those as well.

Navigate to www.json-generator.com and have a look around the site.

Click the 'Help' button and see a list of options we can use to generate data. It has a lot - generating integers, strings, bools, etc. as well as more interesting data generation like lorem ipsum, companies, emails, phone numbers, etc. You can also write custom functions to generate data - which is what we'll do for photos.

We need to generate two sets of data - for males and females (so their names and photos align). And we'll generate 25 of each and then combine them into a single .json file

Ladies first:

[
  '{{repeat(25)}}',             // repeat 25 times
  {
    Username: '{{firstName("female")}}',    // random female first name
    Specialty: '{{random("front", "back", "full")}}',       // choose one of the three options randomly
    DateOfBirth: '{{date(new Date(1975,0,1), new Date(1999, 11, 31), "YYYY-MM-dd")}}',  // random birthday
    Password: 'password',       // all passwords will just be 'password'
    KnownAs: function(){ return this.Username; },   //  will just be their username/firstname
    Created: '{{date(new Date(2017,0,1), new Date(2018, 3, 1), "YYYY-MM-dd")}}',   // dates between 1/1/2017 and 3/1/2018
    LastActive: function(){return this.DateCreated; },    // same as date created
    Introduction: '{{lorem(1, "paragraphs")}}',         // 1 paragraph of lorem ipsum
    LookingFor: '{{lorem(1, "paragraphs")}}',           // 1 paragraph of lorem ipsum
    Interests: '{{lorem(1, "sentences")}}',             // 1 sentence of lorem ipsum
    City: '{{city()}}',        // random city
    State: '{{state()}}',       // random us state
    Photos: [
        {
          url: function(num) {
          return 'https://randomuser.me/api/portraits/women/' + num.integer(1,99) + '.jpg';
        },          // queries this api for a random female photo
        isMain: true,
        description: '{{lorem()}}'
      }
    ]
  }
]

Now, change out 'female' to 'male' in the Username value and change 'women' to 'men' in the api call and run it again:

Username: '{{firstName("male")}}',

//  ...
    return 'https://randomuser.me/api/portraits/men/' + num.integer(1,99) + '.jpg';

Now, combine the two JSON results into a single file.

Alternatively, you can use the JSON data provided in the project GitHub repository.

If you want to verify you combine the two correctly, use a JSON validator like this one.

Save it in your .Data project as UserSeedData.json.

Last updated