I've been a developer for some time now and wanted to try out Jsonata. My data comes from a csv file, where the first row contains the fieldnames. Given the following input json:
[
[
{
"LineNbr" : 1,
"Value" : "Desc1"
},
{
"LineNbr" : 1,
"Value" : "Desc2"
}
],
[
{
"LineNbr" : 2,
"Value" : "Value1"
},
{
"LineNbr" : 2,
"Value" : "Value2"
}
],
[
{
"LineNbr" : 3,
"Value" : "Value3"
},
{
"LineNbr" : 3,
"Value" : "Value4"
}
],
[
{
"LineNbr" : 4,
"Value" : "Value5"
},
{
"LineNbr" : 4,
"Value" : "Value6"
}
],
[
{
"LineNbr" : 5,
"Value" : "Value7"
},
{
"LineNbr" : 5,
"Value" : "Value8"
}
]
]
I would like to create a structure where i map the value of the first row, to each following row inside the array. My desired output would be:
[
{
"Desc1" : "Value1",
"Desc2" : "Value2"
},
{
"Desc1" : "Value3",
"Desc2" : "Value4"
},
{
"Desc1" : "Value5",
"Desc2" : "Value6"
},
{
"Desc1" : "Value7",
"Desc2" : "Value8"
}
]
What i been trying so far:
(
$data := $map($, function($v, $i, $a) {
{
"rowdata": $map([1..$count($v)], function($vs, $is) {$[0][$is].Value})
}
});
)
It brings me kinda in the right direction, as the rowdata mapped value is the correct value. But the structure is messed up. Does anyone have an approach on how to solve this one?
Here's one possible solution: I'm filtering the list of rows to exclude the first one, then map them to an array of objects with the help of the merge function:
(
$firstRow := $$[0];
$dataRows := $$[[1..$count($$)]];
$map($dataRows, function($row) {
$map($row, function($column, $columnIndex) {{
$firstRow[$columnIndex].Value: $column.Value
}}) ~> $merge
})
)
Check it out on the Stedi Playground: https://stedi.link/DJDYwYO