Expert Contributor
Created 10-23-2024 11:21 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@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
- Copies the id field from the original input to the top level of the output.
- Iterates over the categories array and:
- Copies each category object to a new array called categories[].
- If a category contains a subcategories array, each subcategory object is also copied into the same categories[] array, effectively flattening the nested structure.
The second operation
- Searches within each category object in the categories[] array.
- Removes the subcategories field if it exists.
The third operation
- Iterates over the categories[] array, processing each category object.
- Renames the code field to categoryCode and the name field to categoryName for consistency with the new output schema.
- Adds a new field called individualId inside each category object, using the id value from two levels up (the root of the original input).
The fourth operation:
- Takes each object from the categories[] array and moves it to the root level.
- As a result, the categories array wrapper is removed, leaving a flat array of individual objects.
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"