Trying to use JOLT to turn flat objects into arrays - failing miserably :D
HI all
I've been using this community support a LOT lately as I try and get into NiFi and the ETL power it has, and I think it's incredible, but today I find myself instead asking for help, after spending far too long trying to work something out on my own 😕
I'm not too familiar with JOLT and so what I'm trying to do is take a data flow I have which is in the following format:
[
{
"CODE": "1234",
"PRODUCT": "product 1",
"Valid": null,
"Zone 1": "1.00 USD",
"Zone 2": "1.00 USD",
"Zone 3": null,
"Zone 4": null,
"Zone 5": null,
},
{
"CODE": "9876",
"PRODUCT": "product 2",
"Valid": null,
"Zone 1": "18.00 USD",
"Zone 2": "12.00 USD",
"Zone 3": null,
"Zone 4": null,
"Zone 5": null
},
{
"CODE": "3745",
"PRODUCT": "product 3",
"Valid": null,
"Zone 1": "51.00 USD",
"Zone 2": "21.00 USD",
"Zone 3": null,
"Zone 4": null,
"Zone 5": null
}
]
and try to convert it to something like this:
[
{
"code": "1234",
"product": "product 1",
"prices" : [
{
"name" : "Zone 1",
"price": "1.00 USD",
"valid": null
},
{
"name" : "Zone 2",
"price": "1.00 USD",
"valid": null
},
{
"name" : "Zone 3",
"price": null,
"valid": null
},
{
"name" : "Zone 4",
"price": null,
"valid": null
},
{
"name" : "Zone 5",
"price": null,
"valid": null
}
]
},
{
"code": "9876",
"product": "product 2",
"prices" : [
{
"name" : "Zone 1",
"price": "18.00 USD",
"valid": null
},
{
"name" : "Zone 2",
"price": "12.00 USD",
"valid": null
},
{
"name" : "Zone 3",
"price": null,
"valid": null
},
{
"name" : "Zone 4",
"price": null,
"valid": null
},
{
"name" : "Zone 5",
"price": null,
"valid": null
}
]
},
{
"code": "3745",
"product": "product 3",
"prices" : [
{
"name" : "Zone 1",
"price": "51.00 USD",
"valid": null
},
{
"name" : "Zone 2",
"price": "21.00 USD",
"valid": null
},
{
"name" : "Zone 3",
"price": null,
"valid": null
},
{
"name" : "Zone 4",
"price": null,
"valid": null
},
{
"name" : "Zone 5",
"price": null,
"valid": null
}
]
}
]
using my very limited knowledge of JOLT (and incredible search engine skills ha ha) I've managed to get something similar in the playground by trial and error and so I am now at a position like this:
[
{
"code": "1234",
"product": "product 1",
"price_lists": [
{
"Zone 1": "1.00 USD"
},
{
"Zone 2": "1.00 USD"
},
{
"Zone 3": null
},
{
"Zone 4": null
},
{
"Zone 5": null
}
]
},
{
"code": "9876",
"product": "product 2",
"price_lists": [
{
"Zone 1": "18.00 USD"
},
{
"Zone 2": "12.00 USD"
},
{
"Zone 3": null
},
{
"Zone 4": null
},
{
"Zone 5": null
}
]
},
{
"code": "3745",
"product": "product 3",
"price_lists": [
{
"Zone 1": "51.00 USD"
},
{
"Zone 2": "21.00 USD"
},
{
"Zone 3": null
},
{
"Zone 4": null
},
{
"Zone 5": null
}
]
},
null
]
and this is the spec I have used:
[
{
"operation": "shift",
"spec": {
"*": {
"CODE": "[&1].code",
"PRODUCT": "[&1].product",
"Valid": "",
"*": "[&1].price_lists[].&"
}
}
}
]
but sadly I think I have reached the end of my knowledge. I just can't seem to work out how to build the price_lists array in the format that I need it!
I know we can reference hierarchy and also get key values as data using things like "$" and "@" but I simply can't get enough of an example to be able to make sense on how it all works. I may well be going about it the wrong way, but I think this is the right solution, I just don't have the knowledge to be able to achieve what I would like, so I'm reaching out to this amazing community and asking (nay begging) for help ha ha
Thanks in advance all, this is an amazing place !

