<?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 tFileInputJSON access iterating key value in Talend Studio</title>
    <link>https://community.qlik.com/t5/Talend-Studio/tFileInputJSON-access-iterating-key-value/m-p/2300612#M72818</link>
    <description>&lt;P&gt;Hi there,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've a problem transforming some JSON data. My job is as follow:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;tFile -&amp;gt; tFileInputJSON -&amp;gt; ....&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My JSON data are made that way:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;{"Alice": [{"A": 1234, "B": 9876}, {..}], "Bob": [{..}, {..}, {..}]}&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I managed to iterate through all the attributes ('A', 'B' for instance with&amp;nbsp;"$.*[*]" set as loop json query and "@.A") but I didn't find a way to retrieve the value of the key in the JSON path. I want to retrieve "Alice", "Bob", and the others.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My desired result would look like:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Alice | A | 1234&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;Alice | B | 9876&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any idea?&lt;/P&gt;</description>
    <pubDate>Sat, 16 Nov 2024 05:29:57 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2024-11-16T05:29:57Z</dc:date>
    <item>
      <title>tFileInputJSON access iterating key value</title>
      <link>https://community.qlik.com/t5/Talend-Studio/tFileInputJSON-access-iterating-key-value/m-p/2300612#M72818</link>
      <description>&lt;P&gt;Hi there,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've a problem transforming some JSON data. My job is as follow:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;tFile -&amp;gt; tFileInputJSON -&amp;gt; ....&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My JSON data are made that way:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;{"Alice": [{"A": 1234, "B": 9876}, {..}], "Bob": [{..}, {..}, {..}]}&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I managed to iterate through all the attributes ('A', 'B' for instance with&amp;nbsp;"$.*[*]" set as loop json query and "@.A") but I didn't find a way to retrieve the value of the key in the JSON path. I want to retrieve "Alice", "Bob", and the others.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My desired result would look like:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Alice | A | 1234&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;Alice | B | 9876&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any idea?&lt;/P&gt;</description>
      <pubDate>Sat, 16 Nov 2024 05:29:57 GMT</pubDate>
      <guid>https://community.qlik.com/t5/Talend-Studio/tFileInputJSON-access-iterating-key-value/m-p/2300612#M72818</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2024-11-16T05:29:57Z</dc:date>
    </item>
    <item>
      <title>Re: tFileInputJSON access iterating key value</title>
      <link>https://community.qlik.com/t5/Talend-Studio/tFileInputJSON-access-iterating-key-value/m-p/2300613#M72819</link>
      <description>&lt;P&gt;&lt;STRIKE&gt;I found a way around but I believe it's not the best way to achieve my requirement:&lt;/STRIKE&gt;&lt;/P&gt;&lt;P&gt;&lt;STRIKE&gt;&lt;STRONG&gt;tFile -&amp;gt; tFileInputDelimited -&amp;gt; tMap -&amp;gt; ...&amp;nbsp;&lt;/STRONG&gt;&lt;/STRIKE&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Nope&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Jun 2019 14:06:15 GMT</pubDate>
      <guid>https://community.qlik.com/t5/Talend-Studio/tFileInputJSON-access-iterating-key-value/m-p/2300613#M72819</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2019-06-25T14:06:15Z</dc:date>
    </item>
    <item>
      <title>Re: tFileInputJSON access iterating key value</title>
      <link>https://community.qlik.com/t5/Talend-Studio/tFileInputJSON-access-iterating-key-value/m-p/2300614#M72820</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt; 
&lt;P&gt;&amp;nbsp;&lt;/P&gt; 
&lt;P&gt;I don't know if there is an easy way to achieve this but you should be able to produce the desired output by using a mix of Java code.&lt;/P&gt; 
&lt;P&gt;&amp;nbsp;&lt;/P&gt; 
&lt;P&gt;First, load your json file in one global variable (for example, tFileInputFullRow -&amp;gt; tFlowToIterate with empty row delimiter).&lt;/P&gt; 
&lt;P&gt;&amp;nbsp;&lt;/P&gt; 
&lt;P&gt;Once you have the json in memory, you should be able to iterate over the keys with a tJavaFlex, retrieve the needed data with one or more tExtractJSONFields, and finally, build the desired output with a tJavaRow (the tJavaRow is needed to retrieve previous data).&lt;/P&gt; 
&lt;P&gt;&amp;nbsp;&lt;/P&gt; 
&lt;P&gt;Here is a full example :&lt;/P&gt; 
&lt;P&gt;I first load the following json in a "line" global variable&lt;/P&gt; 
&lt;PRE&gt;{"Alice": [{"A": 1234, "B": 9876}, {"A": 4321, "B": 6789}],
"Bob": [{"A": 2468, "B": 9753}, {"A": 8642, "B": 3579}]}&lt;/PRE&gt; 
&lt;P&gt;&amp;nbsp;&lt;/P&gt; 
&lt;P&gt;Then, I use this part of job :&lt;/P&gt; 
&lt;P&gt;&lt;SPAN class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="javaw_20190626_100633_059.png" style="width: 715px;"&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="0683p000009M5ji.png"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/140630i09F3811CE7CBAE79/image-size/large?v=v2&amp;amp;px=999" role="button" title="0683p000009M5ji.png" alt="0683p000009M5ji.png" /&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;/P&gt; 
&lt;P&gt;&amp;nbsp;&lt;/P&gt; 
&lt;P&gt;The tJavaFlex is used to iterate over the keys. It has two output columns (id and data both as String).&lt;/P&gt; 
&lt;P&gt;Import (advanced settings) :&lt;/P&gt; 
&lt;PRE&gt;import java.util.Iterator;
import org.json.JSONArray;
import org.json.JSONObject;&lt;/PRE&gt; 
&lt;P&gt;Initial code :&lt;/P&gt; 
&lt;PRE&gt;String json = ((String)globalMap.get("line")); //retrieve the loaded json
JSONObject j_json = new JSONObject(json); //build a JSONObject from it

Iterator&amp;lt;String&amp;gt; keys = j_json.keys(); //get the keys and loop

while(keys.hasNext()) {&lt;/PRE&gt; 
&lt;P&gt;Main code :&lt;/P&gt; 
&lt;PRE&gt;  String key = keys.next(); // retrieve the key&lt;BR /&gt;  // setup output values&lt;BR /&gt;  row2.id = key; // the id column will be used in the tJavaRow
  row2.data = j_json.getJSONArray(key).toString(); // the data column will be used in the tExtractJSONFields&lt;/PRE&gt; 
&lt;P&gt;End code :&lt;/P&gt; 
&lt;PRE&gt;}&lt;/PRE&gt; 
&lt;P&gt;&amp;nbsp;&lt;/P&gt; 
&lt;P&gt;The tExtractJSONFields retrieve the A and B data (it has 2 output columns A anb B both as String) :&lt;/P&gt; 
&lt;PRE&gt;JSON Fields : data
Loop Jsonpath query : "$.[*]"
Mapping : A="@.A" and B="@.B"&lt;/PRE&gt; 
&lt;P&gt;&amp;nbsp;&lt;/P&gt; 
&lt;P&gt;The tJavaRow build the final dataset (it has 3 columns: id, A and B all Strings) :&lt;/P&gt; 
&lt;PRE&gt;output_row.id = row2.id; // note how the row2 is used here even if it's not the input row
output_row.A = input_row.A; // A and B comes from the previous component
output_row.B = input_row.B;&lt;/PRE&gt; 
&lt;P&gt;&amp;nbsp;&lt;/P&gt; 
&lt;P&gt;Of course, it's possible to use the same method with multiple tExtractJSONFields for nested arrays.&lt;/P&gt; 
&lt;P&gt;The tJavaRow can rebuild the whole output by accessing data from previous rows (using the correct row id as above for the row2).&lt;/P&gt; 
&lt;P&gt;&amp;nbsp;&lt;/P&gt; 
&lt;P&gt;Regards.&lt;/P&gt;</description>
      <pubDate>Wed, 26 Jun 2019 09:19:14 GMT</pubDate>
      <guid>https://community.qlik.com/t5/Talend-Studio/tFileInputJSON-access-iterating-key-value/m-p/2300614#M72820</guid>
      <dc:creator>lennelei</dc:creator>
      <dc:date>2019-06-26T09:19:14Z</dc:date>
    </item>
    <item>
      <title>Re: tFileInputJSON access iterating key value</title>
      <link>https://community.qlik.com/t5/Talend-Studio/tFileInputJSON-access-iterating-key-value/m-p/2300615#M72821</link>
      <description>&lt;P&gt;&lt;A href="https://community.qlik.com/s/profile/0053p000007LMbuAAG"&gt;@lennelei&lt;/A&gt;&amp;nbsp;Thank you so much, that is precise and exact!&lt;/P&gt;</description>
      <pubDate>Wed, 26 Jun 2019 09:52:20 GMT</pubDate>
      <guid>https://community.qlik.com/t5/Talend-Studio/tFileInputJSON-access-iterating-key-value/m-p/2300615#M72821</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2019-06-26T09:52:20Z</dc:date>
    </item>
  </channel>
</rss>

