<?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: How can I convert a fixed width file into Json using controller services? in Support Questions</title>
    <link>https://community.cloudera.com/t5/Support-Questions/How-can-I-convert-a-fixed-width-file-into-Json-using/m-p/378889#M243714</link>
    <description>&lt;P&gt;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/32119"&gt;@VidyaSargur&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;SPAN&gt;Thank you for the warm welcome! I appreciate being a part of this community.&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Sat, 11 Nov 2023 06:34:52 GMT</pubDate>
    <dc:creator>Chai09</dc:creator>
    <dc:date>2023-11-11T06:34:52Z</dc:date>
    <item>
      <title>How can I convert a fixed width file into Json using controller services?</title>
      <link>https://community.cloudera.com/t5/Support-Questions/How-can-I-convert-a-fixed-width-file-into-Json-using/m-p/378839#M243696</link>
      <description>&lt;P&gt;I have an input fixed width file as below&lt;BR /&gt;John 32 M New York 100&lt;BR /&gt;I want to convert it in to a Json like below&lt;BR /&gt;{&lt;BR /&gt;"Name": "John",&lt;BR /&gt;"Age": 32,&lt;BR /&gt;"Gender": "M",&lt;BR /&gt;"City": "New York",&lt;BR /&gt;"Country": 100&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Nov 2023 05:23:06 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/How-can-I-convert-a-fixed-width-file-into-Json-using/m-p/378839#M243696</guid>
      <dc:creator>Chai09</dc:creator>
      <dc:date>2023-11-10T05:23:06Z</dc:date>
    </item>
    <item>
      <title>Re: How can I convert a fixed width file into Json using controller services?</title>
      <link>https://community.cloudera.com/t5/Support-Questions/How-can-I-convert-a-fixed-width-file-into-Json-using/m-p/378841#M243697</link>
      <description>&lt;P&gt;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/107820"&gt;@Chai09&lt;/a&gt;,&amp;nbsp;Welcome to our community! To help you get the best possible answer, I have tagged our NiFi experts&amp;nbsp;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/103151"&gt;@cotopaul&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;&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/95503"&gt;@steven-matison&lt;/a&gt;&amp;nbsp; who may be able to assist you further.&lt;BR /&gt;&lt;BR /&gt;Please feel free to provide any additional information or details about your query, and we hope that you will find a satisfactory solution to your question.&lt;/P&gt;</description>
      <pubDate>Fri, 10 Nov 2023 06:37:00 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/How-can-I-convert-a-fixed-width-file-into-Json-using/m-p/378841#M243697</guid>
      <dc:creator>VidyaSargur</dc:creator>
      <dc:date>2023-11-10T06:37:00Z</dc:date>
    </item>
    <item>
      <title>Re: How can I convert a fixed width file into Json using controller services?</title>
      <link>https://community.cloudera.com/t5/Support-Questions/How-can-I-convert-a-fixed-width-file-into-Json-using/m-p/378882#M243708</link>
      <description>&lt;P&gt;Hi &lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/107820"&gt;@Chai09&lt;/a&gt; ,&lt;/P&gt;&lt;P&gt;There are different options to do this:&lt;/P&gt;&lt;P&gt;1- You can do ExtractText -&amp;gt; AttributeToJson. In the ExtractText you specify the attribute (Name, Age...) and the regex to capture each attribute value. The AttributeToJson processor you can list the attribute that you want to convert into Json in the AttributeList property and set the Destination to "flowfile-content" to get the Json you need.&amp;nbsp; To learn how you to use the ExtractText&amp;nbsp; please refer to:&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.cloudera.com/t5/Support-Questions/How-to-ExtractText-from-flow-file-using-Nifi-Processor/m-p/190826" target="_blank" rel="noopener"&gt;https://community.cloudera.com/t5/Support-Questions/How-to-ExtractText-from-flow-file-using-Nifi-Processor/m-p/190826&lt;/A&gt;&lt;/P&gt;&lt;P&gt;However, You might find the Age and Country values are given to you as string values vs integer, if that is OK with you then you dont need to do anything, If you need them to be integer then you have to use another processor like QueryRecord where you can cast the string into integer or JoltTransformationJson processor where you can do conversion using the following jolt spec:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "Age": "=toInteger(@(1,Age))",
      "Country": "=toInteger(@(1,Country))"
    }
  }
]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2- The easiest way I found if you dont like to use Regex is using QueryRecord with RecordReader set as GrokReader and RecordWrite set as JsonRecordSetWriter as follows&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SAMSAL_0-1699635705346.png" style="width: 400px;"&gt;&lt;img src="https://community.cloudera.com/t5/image/serverpage/image-id/38908iC12EDA0FCFC18194/image-size/medium?v=v2&amp;amp;px=400" role="button" title="SAMSAL_0-1699635705346.png" alt="SAMSAL_0-1699635705346.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;The &lt;STRONG&gt;JsonRecord&lt;/STRONG&gt; is dynamic property that will define the relationship that will produce the Json output you are looking for and it has the following value:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;select Name
,CAST(AgeN as SMALLINT) as Age
,Gender
,City
,CAST(CountryN as SMALLINT) as Country
 from FLOWFILE&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The GrokReader service is used to help you read unstructured data such as log files and its configured as the following:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SAMSAL_2-1699635881126.png" style="width: 400px;"&gt;&lt;img src="https://community.cloudera.com/t5/image/serverpage/image-id/38910iCA526D73892CF76E/image-size/medium?v=v2&amp;amp;px=400" role="button" title="SAMSAL_2-1699635881126.png" alt="SAMSAL_2-1699635881126.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;The Grok Expressions Property is set to the following:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;%{WORD:Name} %{NUMBER:AgeN} %{WORD:Gender} %{DATA:City} %{NUMBER:CountryN}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The Grok Expression uses predefined Regex for the given types: WORD, NUMBER, DATA...etc. For more info: &lt;A href="https://nifi.apache.org/docs/nifi-docs/components/org.apache.nifi/nifi-reaord-serialization-services-nar/1.23.2/org.apache.nifi.grok.GrokReader/additionalDetails.html" target="_blank" rel="noopener"&gt;https://nifi.apache.org/docs/nifi-docs/components/org.apache.nifi/nifi-reaord-serialization-services-nar/1.23.2/org.apache.nifi.grok.GrokReader/additionalDetails.html&lt;/A&gt;&lt;/P&gt;&lt;P&gt;The &lt;STRONG&gt;JsonRecordSetWriter&lt;/STRONG&gt; service is configured as follows :&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SAMSAL_3-1699636047849.png" style="width: 400px;"&gt;&lt;img src="https://community.cloudera.com/t5/image/serverpage/image-id/38911iC8CC82C610FBB686/image-size/medium?v=v2&amp;amp;px=400" role="button" title="SAMSAL_3-1699636047849.png" alt="SAMSAL_3-1699636047849.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;This will produce the Json you are looking for the correct data types. Notice how I needed to still cast the Age,Country to INT despite they are being defined as Number in the Grok Expression and that is because JsonRecordSetWriter will still convert everything to string unless you provide an Avro Schema.&lt;/P&gt;&lt;P&gt;If that helps &lt;STRONG&gt;please&lt;/STRONG&gt; accept solution.&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Nov 2023 17:11:29 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/How-can-I-convert-a-fixed-width-file-into-Json-using/m-p/378882#M243708</guid>
      <dc:creator>SAMSAL</dc:creator>
      <dc:date>2023-11-10T17:11:29Z</dc:date>
    </item>
    <item>
      <title>Re: How can I convert a fixed width file into Json using controller services?</title>
      <link>https://community.cloudera.com/t5/Support-Questions/How-can-I-convert-a-fixed-width-file-into-Json-using/m-p/378889#M243714</link>
      <description>&lt;P&gt;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/32119"&gt;@VidyaSargur&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;SPAN&gt;Thank you for the warm welcome! I appreciate being a part of this community.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 11 Nov 2023 06:34:52 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/How-can-I-convert-a-fixed-width-file-into-Json-using/m-p/378889#M243714</guid>
      <dc:creator>Chai09</dc:creator>
      <dc:date>2023-11-11T06:34:52Z</dc:date>
    </item>
    <item>
      <title>Re: How can I convert a fixed width file into Json using controller services?</title>
      <link>https://community.cloudera.com/t5/Support-Questions/How-can-I-convert-a-fixed-width-file-into-Json-using/m-p/378890#M243715</link>
      <description>&lt;P&gt;Dear &lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/80381"&gt;@SAMSAL&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;I extend my sincere appreciation for your invaluable insights and guidance on the query. Your expertise has been instrumental in implementing the first method, and I'm grateful for your prompt and effective assistance.&lt;/P&gt;&lt;P&gt;Regarding the second method you suggested, it's working seamlessly for our current requirements. However, I'm curious about its applicability to scenarios where a file contains data that requires splitting and segregation.&lt;BR /&gt;&lt;BR /&gt;For Instance, consider the input&lt;BR /&gt;JohnCena32 Male New York USA813668&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;And the desired JSON output:&lt;/SPAN&gt;&lt;BR /&gt;{&lt;BR /&gt;"firstname": "John",&lt;BR /&gt;"lastname": "Cena",&lt;BR /&gt;"age": 32,&lt;BR /&gt;"gender": "Male",&lt;BR /&gt;"city": "New York",&lt;BR /&gt;"country": "USA",&lt;BR /&gt;"mobile": 813668&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;Could you please shed light on whether the second method remains effective in handling data structures like this?&lt;/P&gt;&lt;P&gt;Thank you once again for your time and expertise.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 11 Nov 2023 06:52:42 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/How-can-I-convert-a-fixed-width-file-into-Json-using/m-p/378890#M243715</guid>
      <dc:creator>Chai09</dc:creator>
      <dc:date>2023-11-11T06:52:42Z</dc:date>
    </item>
    <item>
      <title>Re: How can I convert a fixed width file into Json using controller services?</title>
      <link>https://community.cloudera.com/t5/Support-Questions/How-can-I-convert-a-fixed-width-file-into-Json-using/m-p/378892#M243716</link>
      <description>&lt;P&gt;There is no magic solution for those scenarios and no one solution fits all out of Nifi that I can think of. You have to understand the nature of the input before you start consuming it and you have to provide the solution catered to this input. Sometimes if you are lucky you can combine multiple scenarios into one flow but that still depends on the complexity of the input. Even thought in your first scenario the second option I proposed seem to be simple enough and it did the job, for your second example its more complex and I dont think the out of the box GrokReader will be able to handle such complexity, therefore the first option of using the ExtractText Processor will work better because you can customize your regex as needed. For example, based on the text you provided:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;JohnCena32 Male New York USA813668&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can use the following regex:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;[A-Z][a-z]+[A-Z][a-z]+\d+\s(?:Male|Female|M|F)\s[A-Z][a-z]+(?:\s[A-Z][a-z]+)?\s[A-Za-z]+\d+&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the ExtractText processor I will define a dynamic property for each attribute (city, age, firstname...etc.) and surround the segment of the pattern that corresponds to the value with a parenthesis to extract as matching group. For Example:&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000FF"&gt;&lt;SPAN&gt;Age&lt;/SPAN&gt;:&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;[A-Z][a-z]+[A-Z][a-z]+&lt;FONT color="#0000FF"&gt;&lt;STRONG&gt;(\d+)&lt;/STRONG&gt;&lt;/FONT&gt;\s(?:Male|Female|M|F)\s[A-Z][a-z]+(?:\s[A-Z][a-z]+)?\s[A-Za-z]+\d+&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#008000"&gt;FirstName:&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;&lt;FONT color="#008000"&gt;(&lt;STRONG&gt;[A-Z][a-z]+)&lt;/STRONG&gt;&lt;/FONT&gt;[A-Z][a-z]+\d+\s(?:Male|Female|M|F)\s[A-Z][a-z]+(?:\s[A-Z][a-z]+)?\s[A-Za-z]+\d+&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#800080"&gt;Gender:&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;[A-Z][a-z]+[A-Z][a-z]+\d+\s&lt;FONT color="#800080"&gt;&lt;STRONG&gt;((?:Male|Female|M|F))&lt;/STRONG&gt;&lt;/FONT&gt;\s[A-Z][a-z]+(?:\s[A-Z][a-z]+)?\s[A-Za-z]+\d+&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="3" color="#800000"&gt;Country:&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;[A-Z][a-z]+[A-Z][a-z]+\d+\s(?:Male|Female|M|F)\s[A-Z][a-z]+(?:\s[A-Z][a-z]+)?\s&lt;FONT color="#800000"&gt;&lt;STRONG&gt;([A-Za-z]+)&lt;/STRONG&gt;&lt;/FONT&gt;\d+&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;And so on...&lt;/P&gt;&lt;P&gt;This should give you the attribute you need. Then you can use the AttributeToJson processor to get the json output and finally if you want to convert the data to the proper type you can either user JoltTransformation&amp;nbsp; or QueryRecord with cast as shown above.&lt;/P&gt;&lt;P&gt;One final note: If you know how to use some external libraries in python for example or groovy or any of the supported code script in the ExecuteScript processor then you can use that to write your custom code to create the required fllowfile\attributes that will help you downstream to generate the final output.&lt;/P&gt;&lt;P&gt;If that helps please &lt;STRONG&gt;accept&lt;/STRONG&gt; solution.&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 11 Nov 2023 14:53:14 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/How-can-I-convert-a-fixed-width-file-into-Json-using/m-p/378892#M243716</guid>
      <dc:creator>SAMSAL</dc:creator>
      <dc:date>2023-11-11T14:53:14Z</dc:date>
    </item>
  </channel>
</rss>

