Our Community is getting an upgrade! To get everything ready for the relaunch, we’ll be placing the site in read-only mode starting September 21st.
We really appreciate your understanding while we get things set up behind the scenes. Catch up on all the exciting details about the move here.
Need help or have questions? Drop us a line at [email protected]
Created 10-22-2024 02:18 PM
Hi Folks,
This is likely a multipart problem, but I've been trying to get the following sample JSON to distribute the "id" value from the root of the JSON object into a flattened array of objects with the "id" value and the "code" value from each category (or subcategory in the nested structure). For example, given the following JSON payload:
{
"id": "1234",
"categories": [
{
"code": "A",
"name": "Alpha",
"subcategories": [
{
"code": "AA",
"name": "Alpha Alpha"
},
{
"code": "AB",
"name": "Alpha Bravo"
}
]
},
{
"code": "B",
"name": "Bravo"
},
{
"code": "C",
"name": "Charlie"
},
{
"code": "D",
"name": "Delta"
}
]
}
The Desired Output would be:
[
{
"individualId": "1234",
"categoryCode": "A",
"categoryName": "Alpha"
},
{
"individualId": "1234",
"categoryCode": "AA",
"categoryName": "Alpha Alpha"
},
{
"individualId": "1234",
"categoryCode": "AB",
"categoryName": "Alpha Bravo"
},
{
"individualId": "1234",
"categoryCode": "B",
"categoryName": "Bravo"
},
{
"individualId": "1234",
"categoryCode": "C",
"categoryName": "Charlie"
},
{
"individualId": "1234",
"categoryCode": "D",
"categoryName": "Delta"
}
]
I tried the following JOLT Transformation Expression:
[
{
"operation": "shift",
"spec": {
"id": "id",
"categories": {
"*": {
"code": "categories[&1].categoryCode",
"name": "categories[&1].categoryName",
"@(2,id)": "categories[&1].individualId",
"subcategories": {
"*": {
"code": "categories[&3].categoryCode",
"name": "categories[&3].categoryName",
"@(4,id)": "categories[&3].individualId"
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"categories": {
"*": "[]"
}
}
}
]
But got this, not sure why the subcategories keep getting grouped into the first element,. Any insight or help would be much appreciated:
[ {
"individualId" : [ "1234", "1234", "1234" ],
"categoryCode" : [ "A", "AA", "AB" ],
"categoryName" : [ "Alpha", "Alpha Alpha", "Alpha Bravo" ]
}, {
"individualId" : "1234",
"categoryCode" : "B",
"categoryName" : "Bravo"
}, {
"individualId" : "1234",
"categoryCode" : "C",
"categoryName" : "Charlie"
}, {
"individualId" : "1234",
"categoryCode" : "D",
"categoryName" : "Delta"
} ]
Created 10-23-2024 11:21 AM
@MDTechie - Here is a provided solution for the question you asked. I usually like to Jolt Transforms step by step. Please see the Jolt Spec below along with explanations for each step.
[
{
"operation": "shift",
"spec": {
"id": "id",
"categories": {
"*": {
"@": "categories[]",
"subcategories": {
"*": {
"@": "categories[]"
}
}
}
}
}
},
{
"operation": "remove",
"spec": {
"categories": {
"*": {
"subcategories": ""
}
}
}
},
{
"operation": "shift",
"spec": {
"categories": {
"*": {
"code": "categories[&1].categoryCode",
"name": "categories[&1].categoryName",
"@(2,id)": "categories[&1].individualId"
}
}
}
},
{
"operation": "shift",
"spec": {
"categories": {
"*": ""
}
}
}
]
The first operation
The second operation
The third operation
The fourth operation:
I suggest piecing it out in a JOLT tester to understand it a little better. Hope this helps! 🙂
If you found the solution helpful, please "Accept as Solution"
Created 10-22-2024 04:44 PM
@MDTechie Welcome to the Cloudera Community!
To help you get the best possible solution, I have tagged our NiFi experts @MattWho @SAMSAL who may be able to assist you further.
Please keep us updated on your post, and we hope you find a satisfactory solution to your query.
Regards,
Diana Torres,Created 10-23-2024 06:28 AM
Hi @DianaTorres , thank you for the warm welcome. Appreciate you tagging the cavalry!
Best,
Created 10-23-2024 11:21 AM
@MDTechie - Here is a provided solution for the question you asked. I usually like to Jolt Transforms step by step. Please see the Jolt Spec below along with explanations for each step.
[
{
"operation": "shift",
"spec": {
"id": "id",
"categories": {
"*": {
"@": "categories[]",
"subcategories": {
"*": {
"@": "categories[]"
}
}
}
}
}
},
{
"operation": "remove",
"spec": {
"categories": {
"*": {
"subcategories": ""
}
}
}
},
{
"operation": "shift",
"spec": {
"categories": {
"*": {
"code": "categories[&1].categoryCode",
"name": "categories[&1].categoryName",
"@(2,id)": "categories[&1].individualId"
}
}
}
},
{
"operation": "shift",
"spec": {
"categories": {
"*": ""
}
}
}
]
The first operation
The second operation
The third operation
The fourth operation:
I suggest piecing it out in a JOLT tester to understand it a little better. Hope this helps! 🙂
If you found the solution helpful, please "Accept as Solution"
Created on 10-23-2024 12:49 PM - edited 10-23-2024 12:51 PM
Hi @drewski7 ,
Now I have another minor problem.. how do I send you a six pack to thank you for the help??? 😉
Been banging my head on this for days, still going through the logic to learn from it. Really appreciate the detailed explanation. 👍 🙂
Cheers! :clinking_beer_mugs: