The JSONata array constructor

Published on April 4, 2025

Evaluating the same JSONata expression with dynamic JSON inputs often leads to inconsistent resulting structures. To make the result of a JSONata expression conform to only one type of structure, usually an array of objects, you need to use the JSONata array constructor.

JSONata array constructor

This constructor is called by placing square brackets ([]) anywhere on a location path.

For example, given this array of objects as input:

[
  {
    "id": "A",
    "price": 10
  },
  {
    "id": "B",
    "price": 20
  },
  {
    "id": "C",
    "price": 30
  }
]

an expression that filters the array by price:

$[price > 25]

will output a single object:

{
  "id": "C",
  "price": 30
}

although a filter that matches more than one object:

$[price > 15]

will output an array:

[
  {
    "id": "B",
    "price": 20
  },
  {
    "id": "C",
    "price": 30
  }
]

To force the expression to always output an array of objects, prefix or suffix the filter with []:

$[][price > 25]
$[price > 25][]

or surround the entire path:

[$[price > 25]]

which will consistenly build an array structure:

[
  {
    "id": "C",
    "price": 30
  }
]

Useful resources

To learn more about array contructors and building result structures, visit the official documentation at https://docs.jsonata.org/construction.