The function items
is used for taking an object’s key-value pairs and transforming them into an array of arrays, with each array consisting of the key-value pair’s key and value.
Syntax
array items(json_object $object)
Creates an array of arrays based on the key-value pairs from $object.
Parameter
$object The JSON object that should be split into an array of arrays.
Example
{
"obj": {
"b": "dataB",
"c": "dataC",
"d": "dataD"
}
}
{
itemsArr: items(obj)
}
{
"itemsArr": [
[
"b",
"dataB"
],
[
"c",
"dataC"
],
[
"d",
"dataD"
]
]
}
In this example, the object obj
is transformed from being an object into an array of arrays containing both the key and value in each array.
Notes
Reaching the value of the key
Usually, it is not possible to use the key as a value. However, with the items
function, you can use an array operator to output the key of a key-value pair. For example, with the result above, you can reach the key “b” with itemsArr[0][0]
. In a transformation, it could look like this:
{
itemsArr: items(obj),
reachB: $.itemsArr[0][0]
}
{
"itemsArr": [
[
"b",
"dataB"
],
[
"c",
"dataC"
],
[
"d",
"dataD"
]
],
"reachB": "b"
}