Reputation: 65
I've already created a spec to convert my JSON input
{
"rows": [
{
"row": [
"row1",
"row2",
"row3"
],
"header": [
"header1",
"header2",
"header3"
]
},
{
"row": [
"row4",
"row5",
"row6"
],
"header": [
"header4",
"header5",
"header6"
]
}
]
}
to convert to key-value pairs as following object result :
{
"header1" : "row1",
"header2" : "row2",
"header3" : "row3",
"header4" : "row4",
"header5" : "row5",
"header6" : "row6"
}
Is this possible to do using Jolt?
Upvotes: 1
Views: 341
Reputation: 65408
One option is to use the following shift transformation spec :
[
{
"operation": "shift",
"spec": {
"*s": { // rows array
"*": {
"&(1,1)": { // row array
"*": {
"@": "@(3,header[&1])"
}
}
}
}
}
}
]
where
"*s": {
stands for rows"&(1,1)": {
-> not immediate(zeroth) level but one more level up by using &(1,
and grab the value there the first asterisk exists by &(..,1)
"@": "@(3,header[&1])"
-> 3
levels needed as stated at the right hand side traverse the colon
as well in order to reach the level of &(1,1)
which is used to
represent the "row"
array along with &1
representation to go one level up the tree to reach the indexes of the array "row"
while matching with the values of "row"
through use of @
on the left hand sidethe demo on the site http://jolt-demo.appspot.com/ is :
Upvotes: 1
Reputation: 12093
Is there a copy/paste error in your input? Judging by your desired output, the second object's header
array should be ["header4", "header5", "header6"]
. If that's the case, this spec should work:
[
{
"operation": "shift",
"spec": {
"rows": {
"*": {
"header": {
"*": {
"*": {
"@(3,row[#2])": "&"
}
}
}
}
}
}
}
]
Upvotes: 1