<?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>topic Read and write multiple JSON files with different structure in Talend Studio</title>
    <link>https://community.qlik.com/t5/Talend-Studio/Read-and-write-multiple-JSON-files-with-different-structure/m-p/2469263#M140955</link>
    <description>&lt;P&gt;Hi experts,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have more than 30 JSON files, each with a different structure. I created the metadata for each file, and now I want to know if there is a dynamic way of reading all at once.&lt;/P&gt;
&lt;P&gt;I know the idea of iterating using the tfilelist, but I wanted to know if it is possible to make my schema dynamic somehow. Each json has a different schema.&lt;/P&gt;
&lt;P&gt;I read somewhere that it wasn`t possible but I wanted to confirm with you, the experts.&lt;/P&gt;
&lt;P&gt;Best,&lt;/P&gt;
&lt;P&gt;Fernando Vizcaino&lt;/P&gt;</description>
    <pubDate>Tue, 09 Jul 2024 16:50:56 GMT</pubDate>
    <dc:creator>fevizcaino</dc:creator>
    <dc:date>2024-07-09T16:50:56Z</dc:date>
    <item>
      <title>Read and write multiple JSON files with different structure</title>
      <link>https://community.qlik.com/t5/Talend-Studio/Read-and-write-multiple-JSON-files-with-different-structure/m-p/2469263#M140955</link>
      <description>&lt;P&gt;Hi experts,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have more than 30 JSON files, each with a different structure. I created the metadata for each file, and now I want to know if there is a dynamic way of reading all at once.&lt;/P&gt;
&lt;P&gt;I know the idea of iterating using the tfilelist, but I wanted to know if it is possible to make my schema dynamic somehow. Each json has a different schema.&lt;/P&gt;
&lt;P&gt;I read somewhere that it wasn`t possible but I wanted to confirm with you, the experts.&lt;/P&gt;
&lt;P&gt;Best,&lt;/P&gt;
&lt;P&gt;Fernando Vizcaino&lt;/P&gt;</description>
      <pubDate>Tue, 09 Jul 2024 16:50:56 GMT</pubDate>
      <guid>https://community.qlik.com/t5/Talend-Studio/Read-and-write-multiple-JSON-files-with-different-structure/m-p/2469263#M140955</guid>
      <dc:creator>fevizcaino</dc:creator>
      <dc:date>2024-07-09T16:50:56Z</dc:date>
    </item>
    <item>
      <title>Re: Read and write multiple JSON files with different structure</title>
      <link>https://community.qlik.com/t5/Talend-Studio/Read-and-write-multiple-JSON-files-with-different-structure/m-p/2469330#M140957</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;
&lt;P&gt;&lt;CODE&gt;JsonGet&lt;/CODE&gt; can be used in this scenario to dynamically extract values from JSON files. Here's how you can modify the script to use &lt;CODE&gt;JsonGet&lt;/CODE&gt; for handling different JSON schemas:&lt;/P&gt;
&lt;H3&gt;Step-by-Step Solution Using &lt;CODE&gt;JsonGet&lt;/CODE&gt;:&lt;/H3&gt;
&lt;OL&gt;
&lt;LI&gt;&lt;STRONG&gt;Iterate through the files&lt;/STRONG&gt;: Use a FOR EACH loop to iterate through all JSON files in the directory.&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Load JSON content&lt;/STRONG&gt;: Dynamically load each JSON file content.&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Use &lt;CODE&gt;JsonGet&lt;/CODE&gt; to extract values&lt;/STRONG&gt;: Dynamically extract JSON values using &lt;CODE&gt;JsonGet&lt;/CODE&gt;.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;Here is an example script that demonstrates this approach using &lt;CODE&gt;JsonGet&lt;/CODE&gt;:&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Load Script &lt;/SPAN&gt;&lt;/P&gt;
&lt;DIV class="dark bg-gray-950 rounded-md border-[0.5px] border-token-border-medium"&gt;
&lt;DIV class="overflow-y-auto p-4" dir="ltr"&gt;&lt;CODE class="!whitespace-pre hljs language-qlik"&gt;SET vDir = 'lib://YourDataConnection/YourDirectory/';

FOR EACH vFile IN FileList('$(vDir)*.json')

    // Load the entire JSON file as a single field
    JsonContent:
    LOAD
        @1 as JsonString
    FROM [$(vFile)] (txt, utf8);

    // Parse the JSON string dynamically
    FOR i = 0 TO NoOfRows('JsonContent') - 1
        LET vJsonString = Peek('JsonString', i, 'JsonContent');
        
        // Example: Dynamically extracting known keys (customize as needed)
        LET vKey1 = JsonGet(vJsonString, '$.key1');
        LET vKey2 = JsonGet(vJsonString, '$.key2');
        LET vKey3 = JsonGet(vJsonString, '$.key3');

        // Load extracted values into a table
        DynamicJsonTable:
        LOAD
            '$(vKey1)' as Key1,
            '$(vKey2)' as Key2,
            '$(vKey3)' as Key3
        AUTOGENERATE 1;
    NEXT i;

    // Drop temporary table
    DROP TABLE JsonContent;

NEXT vFile;
&lt;/CODE&gt;&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;H3&gt;Explanation:&lt;/H3&gt;
&lt;OL&gt;
&lt;LI&gt;&lt;STRONG&gt;Directory Setup&lt;/STRONG&gt;: The &lt;CODE&gt;SET vDir&lt;/CODE&gt; statement sets the directory path where your JSON files are located.&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;File Iteration&lt;/STRONG&gt;: The &lt;CODE&gt;FOR EACH vFile IN FileList('$(vDir)*.json')&lt;/CODE&gt; loop iterates through each JSON file in the directory.&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Loading JSON Content&lt;/STRONG&gt;: Each JSON file is loaded as a single string field in the &lt;CODE&gt;JsonContent&lt;/CODE&gt; table.&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Parsing JSON Content&lt;/STRONG&gt;: For each row in the &lt;CODE&gt;JsonContent&lt;/CODE&gt; table, the script parses the JSON string using &lt;CODE&gt;JsonGet&lt;/CODE&gt; function.&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Dynamic Table Creation&lt;/STRONG&gt;: The parsed values are loaded into a dynamic table &lt;CODE&gt;[DynamicJsonTable]&lt;/CODE&gt;.&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Clean Up&lt;/STRONG&gt;: Temporary tables are dropped to clean up the data model.&lt;/LI&gt;
&lt;/OL&gt;
&lt;H3&gt;Notes:&lt;/H3&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;STRONG&gt;JsonGet Function&lt;/STRONG&gt;: The &lt;CODE&gt;JsonGet&lt;/CODE&gt; function is used to extract values from the JSON string. You need to specify the JSON path to the values you want to extract (e.g., &lt;CODE&gt;$.key1&lt;/CODE&gt;).&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Customizing Extraction&lt;/STRONG&gt;: Customize the script to handle your specific JSON paths and keys. If your JSON schemas vary significantly, you may need to add more logic to handle different structures.&lt;/LI&gt;
&lt;/UL&gt;
&lt;H3&gt;&lt;FONT color="#FF00FF"&gt;Handling Different Schemas:&lt;/FONT&gt;&lt;/H3&gt;
&lt;P&gt;If your JSON files have widely varying schemas, consider adding conditional logic to handle different structures. For example:&lt;/P&gt;
&lt;DIV class="dark bg-gray-950 rounded-md border-[0.5px] border-token-border-medium"&gt;
&lt;DIV class="flex items-center relative text-token-text-secondary bg-token-main-surface-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md"&gt;
&lt;DIV class="flex items-center"&gt;&lt;SPAN class="" data-state="closed"&gt;Load Script &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;DIV class="overflow-y-auto p-4" dir="ltr"&gt;&lt;CODE class="!whitespace-pre hljs language-qlik"&gt;IF WildMatch(vJsonString, '*"specificKey"*') THEN
    LET vSpecificValue = JsonGet(vJsonString, '$.specificKey');
    // Handle specific schema
    SpecificJsonTable:
    LOAD
        '$(vSpecificValue)' as SpecificValue
    AUTOGENERATE 1;
ELSE
    // Handle generic schema
    GenericJsonTable:
    LOAD
        '$(vKey1)' as Key1,
        '$(vKey2)' as Key2,
        '$(vKey3)' as Key3
    AUTOGENERATE 1;
ENDIF;
&lt;/CODE&gt;&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;P&gt;&lt;LI-WRAPPER&gt;&lt;/LI-WRAPPER&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;This approach allows you to dynamically handle different JSON schemas based on the presence of specific keys or structures. Customize the script further based on the specific variations in your JSON files.&lt;/EM&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Jul 2024 23:55:34 GMT</pubDate>
      <guid>https://community.qlik.com/t5/Talend-Studio/Read-and-write-multiple-JSON-files-with-different-structure/m-p/2469330#M140957</guid>
      <dc:creator>Scotchy</dc:creator>
      <dc:date>2024-07-09T23:55:34Z</dc:date>
    </item>
  </channel>
</rss>

