My JSON object has keys that are strings that are dynamically generated with the json is created. Trying to access the first key doesn't seem to work.
This is my jsonata expression
(
$firstDate := $keys(slice)[0];
$.{
"firstDate": $firstDate,
"x": slice.$firstDate,
"y": slice."Friday, February 28"
}
)
And this is my input JSON
{
"slice": {
"Friday, February 28": [
{
"key": "friday"
},
{
"key": "february"
}
]
}
}
I expect "x" to have the same value as "y".
{
"firstDate": "Friday, February 28",
"x": "Friday, February 28",
"y": [
{
"key": "friday"
},
{
"key": "february"
}
]
}
Use the lookup()
function. In JSONata Object functions
Signature: $lookup(object, key)
Returns the value associated with key in object. If the first argument is an array of objects, then all of the objects in the array are searched, and the values associated with all occurrences of key are returned.
(
$firstDate := $keys(slice)[0];
$.{
"firstDate": $firstDate,
"x": $lookup(slice, $firstDate),
"y": slice."Friday, February 28"
}
)
The result
{
"firstDate": "Friday, February 28",
"x": [
{
"key": "friday"
},
{
"key": "february"
}
],
"y": [
{
"key": "friday"
},
{
"key": "february"
}
]
}