Fork me on GitHub

mockJSON

Example

Template JSON


            

Random generated JSON

Reload to see newly generated data


        

MockJSON is a plugin for jQuery that can hijack JSON and JSONP requests and respond with randomly generated JSON data. Completely random JSON would be pretty useless of course, so you can specify a JSON template for each request. On the right you see an example of such a template and some generated output.

GitHub | try it

Usage

// create template and regular expression for request that
// should be mocked
$.mockJSON(/foo\.json/, {
    "fathers|5-10":[ ...etc... ]
});

// request JSON as usual
$.getJSON('foo/foo.json', function(json) {
    // this will be mocked
});

$.getJSON('foo/bar.json', function(json) {
    // this will not be mocked
});

A template is valid JSON in itself and as such is an object with labels and values. Values can be strings, numbers, booleans, arrays and other objects.

Numbers

Adding |x-y to a number's label will replace the number with r, where r is a random number between x and y.

// Example template
{ "age|0-99" : 0 }

// Example output
{ "age" : 42 }

Adding |+x to a number's label, will increment that number with x for each new item. This can be useful for sequential ID's, etc.

Boolean

Adding |x-y to a boolean's label will randomly replace the boolean with true or false. The values of x and y are irrelevant.

// Example template
{ "married|0-1" : true }

// Example output
{ "married" : false }

Strings

MockJSON comes with a number of keywords in the form @KEY_WORD that are automatically replaced with random data inside a string. These keywords can be combined with other characters in a string.

// Example template
{ "name" : "@LAST_NAME, @MALE_FIRST_NAME" }

// Example output
{ "name" : "Hall, Kevin" }

You can easily add your own keywords.

// Adding the @US_STATE keyword
$.mockJSON.data.US_STATE = [
    'Alabama', 'Alaska', ... , 'Wisconsin', 'Wyoming'
];

// Example template
{ "state" : "@US_STATE" }

// Example output
{ "state" : "Alaska" }

Adding |x-y to a string's label will repeat the string r times, where r is a random number between x and y.

// Example template
{ "rating|1-5" : "*" }

// Example output
{ "rating" : "***" }


// This can also be combined with keywords.
{ "initials|1-5" : "@LETTER_UPPER." }

// Example output
{ "initials" : "V.M.J." }

Arrays

Adding |x-y to an array's label will fill the array with r instances of the first element, where r is a random number between x and y. Each of these elements will be treated as a new template.

// Example template
{
    "daughters|0-3" : [
        { "Name" : "@FEMALE_FIRST_NAME" }
    ],
    "sons|0-3" : ["@MALE_FIRST_NAME"]
} 

// Example output
{
    "daughters" : [
        { "Name" : "Linda" },
        { "Name" : "Barbara" }
    ],
    "sons" : ["William", "David", "Jeffrey"]
} 

Extra's

Sometimes random is a bit too random. When you want to have predictable, reproducible JSON generated: set the $.mockJSON.random = false.