back

Mockaroo tips and tricks

mock

Mockaroo comes with an intuitive interface to generate data. For example, to create an email address, we can put an email into the Field Name, select Email Address from Type, then choose how many rows we want to generate.

Most of the Types are self-explanatory and come with examples. Some of the slightly advanced use cases for data generation are below:

Use Hidden Field

Put a double underscore in front of the Field Name to prevent it from showing (but it can still be referenced).

hidden_field

Use Reference Field

Use the Template type and curly brackets for reference.

For example, if we want to generate PROGRAM_1, PROGRAM_2, PROGRAM_3

reference_field

Append Value to a Number

Use the formula:

"PROGRAM_" + this.to_s
value_to_number

Use Foreign Key

We can link a field from another table through Dataset Column.

For example, if we want to link a Donor to the Program:

  1. Create a Program schema, then click Create Dataset (small arrow next to the Download Data button).
  2. Create a Donor schema, choose Dataset Column, and the field to reference.
foreign_key

Use Nested Object

Refer to Mockaroo’s docs here: Mockaroo JSON Help

For example, if we want gender to contain a list of objects:

"gender": [
  {
    "sex": "Male",
    "count": 303
  },
  {
    "sex": "Female",
    "count": 356
  }
]
nested_object

gender.sex Formula

if this == 1 then 'Male'
else 'Female' end

Use Custom List

Method 1: Use the built-in Type Custom List.

custom_list

Method 2: Use Template and click on the Sigma button at the end.

custom_list_2

Put the name list in the Formula area with the random function from 0 to last, for example:

[
  "mg/m2",
  "IU/m2",
  "ug/m2",
  "g/m2",
  "mg/kg"
][random(0,4)]

Use Regex

Refer to Mockaroo’s docs: Mockaroo Regular Expressions Help

For example, if we want to generate comorbidity_type_code like E10, C50.1, I11, M06

regex

Use Start and End Date

We can create a formula to make sure the end date comes after the start date.

start_end_date

In the Formula (Sigma button), put in:

birth_year = year(date_of_birth)
curr_year = year(now())
life_span = curr_year - birth_year
date_of_birth + years(random(1, life_span))

Use Random Number

The Number Type is random by default, just give it a range (min-max) and it will generate a random number.

start_end_date

Use Condition to Show Value

Use the Formula (Sigma button) to check for an if condition, then put the value after then.

For example, if we want to show date_of_death only if is_deceased is true:

start_end_date

Inside the date_of_death formula:

birth_year = year(date_of_birth)
curr_year = year(now())
life_span = curr_year - birth_year
death_year = date_of_birth + years(random(1, life_span))

if is_deceased == true
then death_year
else nil end

Another example, if we want to show date_of_birth.day_interval based on the condition of date_resolution:

if date_resolution == 'day' && !field('date_of_birth.month_interval').nil?
then field('date_of_birth.month_interval') * 30
else nil end

These tips should help you get more out of Mockaroo. Explore the options and see how they fit into your projects!

©  2024 Son Chau