Fake.REST logo
Fake.REST
Topics
Getting started
Dynamic templating
Scripting

Array

The array templates allow generating an array of any JSON object. For array generation, the whole template should be a valid JSON object.

Template definitions

Here is an example of a template to generate an array with 3 elements of a string.

{
  "[3]":"This is string"
}

The result will be a JSON:

[
  "This is string",
  "This is string",
  "This is string"
]

Instead of a string, you can put any JSON type. For example the object with another template:

{
  "[3]":{
    "name": "{{name.firstName()}}",
    "lastName": "{{name.lastName()}}"
  }
}

The result will be similar like:

[
  { "name": "Ebony", "lastName": "Gleichner" },
  { "name": "Icie", "lastName": "Oberbrunner" },
  { "name": "Carlotta", "lastName": "Emmerich" }
]

When the random length of an array is needed you can use [3..6] syntax. This will generate an array with random length from 3 to 6

Array generation could be used to fill any field of JSON:

{
  "status": "ok",
  "users": {
    "[3..5]": {
      "name": "{{name.firstName()}}",
      "lastName": "{{name.lastName()}}"
    }
  },
  "newUsers": {
    "[2..4]": {
      "name": "{{name.firstName()}}",
      "lastName": "{{name.lastName()}}"
    }
  }
}

The result will be similar to:

{
  "status": "ok",
  "users": [
    { "name": "Lon", "lastName": "Greenfelder" },
    { "name": "Gaston", "lastName": "Feil" },
    { "name": "Edwardo", "lastName": "Corwin" }
  ],
  "newUsers": [
    { "name": "Lula", "lastName": "Jacobi" },
    { "name": "Issac", "lastName": "Beahan" }
  ]
}

Array generators also support the nesting. For example, this will returns the nested arrays. Please be careful with this to prevent the generation of a big amount of data. All arrays are limited to 500 elements in sum.

{
  "data": {
    "[2..3]": {
      "[1..2]" : {
        "x": 10,
        "y": 20,
      }
    }
  }
}

The result will be similar to:

{
  "data": [
    [ { "x": 10, "y": 20 }, { "x": 10, "y": 20 } ],
    [ { "x": 10, "y": 20 }, { "x": 10, "y": 20 } ],
    [ { "x": 10, "y": 20 }, { "x": 10, "y": 20 } ]
  ]
}

Please note in the nested templates the generation process first generates the deepest ones and then populates the upper templates with results.