<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>question Re: Apache NiFi  - JOLT Expression: Flatten Hierarchical Data &amp;amp; Distribute Value from JSON into Array of Objects in Support Questions</title>
    <link>https://community.cloudera.com/t5/Support-Questions/Apache-NiFi-JOLT-Expression-Flatten-Hierarchical-Data-amp/m-p/395857#M249044</link>
    <description>&lt;P&gt;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/112613"&gt;@MDTechie&lt;/a&gt;&amp;nbsp;- 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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;[
  {
    "operation": "shift",
    "spec": {
      "id": "id",
      "categories": {
        "*": {
          "@": "categories[]",
          "subcategories": {
            "*": {
              "@": "categories[]"
            }
          }
        }
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "categories": {
        "*": {
          "subcategories": ""
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "categories": {
        "*": {
          "code": "categories[&amp;amp;1].categoryCode",
          "name": "categories[&amp;amp;1].categoryName",
          "@(2,id)": "categories[&amp;amp;1].individualId"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "categories": {
        "*": ""
      }
    }
  }
]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;The first operation&lt;/U&gt;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Copies the id field from the original input to the top level of the output.&lt;/LI&gt;&lt;LI&gt;Iterates over the categories array and:&lt;UL&gt;&lt;LI&gt;Copies each category object to a new array called categories[].&lt;/LI&gt;&lt;LI&gt;If a category contains a subcategories array, each subcategory object is also copied into the same categories[] array, effectively flattening the nested structure.&lt;/LI&gt;&lt;/UL&gt;&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&lt;U&gt;The second operation&lt;/U&gt;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Searches within each category object in the categories[] array.&lt;/LI&gt;&lt;LI&gt;Removes the subcategories field if it exists.&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&lt;U&gt;The third operation&lt;/U&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Iterates over the categories[] array, processing each category object.&lt;/LI&gt;&lt;LI&gt;Renames the code field to categoryCode and the name field to categoryName for consistency with the new output schema.&lt;/LI&gt;&lt;LI&gt;Adds a new field called individualId inside each category object, using the id value from two levels up (the root of the original input).&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&lt;U&gt;The fourth operation:&lt;/U&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Takes each object from the categories[] array and moves it to the root level.&lt;/LI&gt;&lt;LI&gt;As a result, the categories array wrapper is removed, leaving a flat array of individual objects.&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;I suggest piecing it out in a JOLT tester to understand it a little better. Hope this helps! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;If you found the solution helpful, please &lt;U&gt;&lt;STRONG&gt;"Accept as Solution"&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
    <pubDate>Wed, 23 Oct 2024 18:21:38 GMT</pubDate>
    <dc:creator>drewski7</dc:creator>
    <dc:date>2024-10-23T18:21:38Z</dc:date>
    <item>
      <title>Apache NiFi  - JOLT Expression: Flatten Hierarchical Data &amp; Distribute Value from JSON into Array of Objects</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Apache-NiFi-JOLT-Expression-Flatten-Hierarchical-Data-amp/m-p/395770#M249023</link>
      <description>&lt;P&gt;Hi Folks,&lt;BR /&gt;&lt;BR /&gt;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).&amp;nbsp; For example, given the following JSON payload:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;{
  "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"
    }
  ]
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The Desired Output would be:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;[
  {
    "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"
  }
]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried the following JOLT Transformation Expression:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;[
  {
    "operation": "shift",
    "spec": {
      "id": "id",
      "categories": {
        "*": {
          "code": "categories[&amp;amp;1].categoryCode",
          "name": "categories[&amp;amp;1].categoryName",
          "@(2,id)": "categories[&amp;amp;1].individualId",
          "subcategories": {
            "*": {
              "code": "categories[&amp;amp;3].categoryCode",
              "name": "categories[&amp;amp;3].categoryName",
              "@(4,id)": "categories[&amp;amp;3].individualId"
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "categories": {
        "*": "[]"
      }
    }
  }
]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But got this, not sure why the subcategories keep getting grouped into the first element,. Any insight or help would be much appreciated:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;[ {
  "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"
} ]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Oct 2024 21:18:51 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Apache-NiFi-JOLT-Expression-Flatten-Hierarchical-Data-amp/m-p/395770#M249023</guid>
      <dc:creator>MDTechie</dc:creator>
      <dc:date>2024-10-22T21:18:51Z</dc:date>
    </item>
    <item>
      <title>Re: Apache NiFi  - JOLT Expression: Flatten Hierarchical Data &amp; Distribute Value from JSON into Array of Objects</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Apache-NiFi-JOLT-Expression-Flatten-Hierarchical-Data-amp/m-p/395775#M249024</link>
      <description>&lt;P&gt;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/112613"&gt;@MDTechie&lt;/a&gt;&amp;nbsp;Welcome to the Cloudera Community!&lt;BR /&gt;&lt;BR /&gt;To help you get the best possible solution, I have tagged our NiFi experts&amp;nbsp;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/35454"&gt;@MattWho&lt;/a&gt;&amp;nbsp;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/80381"&gt;@SAMSAL&lt;/a&gt;&amp;nbsp; who may be able to assist you further.&lt;BR /&gt;&lt;BR /&gt;Please keep us updated on your post, and we hope you find a satisfactory solution to your query.&lt;/P&gt;</description>
      <pubDate>Tue, 22 Oct 2024 23:44:18 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Apache-NiFi-JOLT-Expression-Flatten-Hierarchical-Data-amp/m-p/395775#M249024</guid>
      <dc:creator>DianaTorres</dc:creator>
      <dc:date>2024-10-22T23:44:18Z</dc:date>
    </item>
    <item>
      <title>Re: Apache NiFi  - JOLT Expression: Flatten Hierarchical Data &amp; Distribute Value from JSON into Array of Objects</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Apache-NiFi-JOLT-Expression-Flatten-Hierarchical-Data-amp/m-p/395822#M249035</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/93628"&gt;@DianaTorres&lt;/a&gt;&amp;nbsp;, thank you for the warm welcome.&amp;nbsp; Appreciate you tagging the cavalry!&lt;/P&gt;&lt;P&gt;Best,&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 23 Oct 2024 13:28:59 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Apache-NiFi-JOLT-Expression-Flatten-Hierarchical-Data-amp/m-p/395822#M249035</guid>
      <dc:creator>MDTechie</dc:creator>
      <dc:date>2024-10-23T13:28:59Z</dc:date>
    </item>
    <item>
      <title>Re: Apache NiFi  - JOLT Expression: Flatten Hierarchical Data &amp; Distribute Value from JSON into Array of Objects</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Apache-NiFi-JOLT-Expression-Flatten-Hierarchical-Data-amp/m-p/395857#M249044</link>
      <description>&lt;P&gt;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/112613"&gt;@MDTechie&lt;/a&gt;&amp;nbsp;- 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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;[
  {
    "operation": "shift",
    "spec": {
      "id": "id",
      "categories": {
        "*": {
          "@": "categories[]",
          "subcategories": {
            "*": {
              "@": "categories[]"
            }
          }
        }
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "categories": {
        "*": {
          "subcategories": ""
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "categories": {
        "*": {
          "code": "categories[&amp;amp;1].categoryCode",
          "name": "categories[&amp;amp;1].categoryName",
          "@(2,id)": "categories[&amp;amp;1].individualId"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "categories": {
        "*": ""
      }
    }
  }
]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;The first operation&lt;/U&gt;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Copies the id field from the original input to the top level of the output.&lt;/LI&gt;&lt;LI&gt;Iterates over the categories array and:&lt;UL&gt;&lt;LI&gt;Copies each category object to a new array called categories[].&lt;/LI&gt;&lt;LI&gt;If a category contains a subcategories array, each subcategory object is also copied into the same categories[] array, effectively flattening the nested structure.&lt;/LI&gt;&lt;/UL&gt;&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&lt;U&gt;The second operation&lt;/U&gt;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Searches within each category object in the categories[] array.&lt;/LI&gt;&lt;LI&gt;Removes the subcategories field if it exists.&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&lt;U&gt;The third operation&lt;/U&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Iterates over the categories[] array, processing each category object.&lt;/LI&gt;&lt;LI&gt;Renames the code field to categoryCode and the name field to categoryName for consistency with the new output schema.&lt;/LI&gt;&lt;LI&gt;Adds a new field called individualId inside each category object, using the id value from two levels up (the root of the original input).&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&lt;U&gt;The fourth operation:&lt;/U&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Takes each object from the categories[] array and moves it to the root level.&lt;/LI&gt;&lt;LI&gt;As a result, the categories array wrapper is removed, leaving a flat array of individual objects.&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;I suggest piecing it out in a JOLT tester to understand it a little better. Hope this helps! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;If you found the solution helpful, please &lt;U&gt;&lt;STRONG&gt;"Accept as Solution"&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 23 Oct 2024 18:21:38 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Apache-NiFi-JOLT-Expression-Flatten-Hierarchical-Data-amp/m-p/395857#M249044</guid>
      <dc:creator>drewski7</dc:creator>
      <dc:date>2024-10-23T18:21:38Z</dc:date>
    </item>
    <item>
      <title>Re: Apache NiFi  - JOLT Expression: Flatten Hierarchical Data &amp; Distribute Value from JSON into Array of Objects</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Apache-NiFi-JOLT-Expression-Flatten-Hierarchical-Data-amp/m-p/395862#M249049</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/79092"&gt;@drewski7&lt;/a&gt;&amp;nbsp;,&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Now I have another minor problem.. how do I send you a six pack to thank you for the help??? &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;BR /&gt;&lt;BR /&gt;Been banging my head on this for days, still going through the logic to learn from it.&amp;nbsp; Really appreciate the detailed explanation.&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":thumbs_up:"&gt;👍&lt;/span&gt; &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;&lt;BR /&gt;Cheers! :clinking_beer_mugs:&lt;/img&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 23 Oct 2024 19:51:17 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Apache-NiFi-JOLT-Expression-Flatten-Hierarchical-Data-amp/m-p/395862#M249049</guid>
      <dc:creator>MDTechie</dc:creator>
      <dc:date>2024-10-23T19:51:17Z</dc:date>
    </item>
  </channel>
</rss>

