<?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 Re: How do you import JSONs that contain double nested arrays? in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/How-do-you-import-JSONs-that-contain-double-nested-arrays/m-p/534353#M75694</link>
    <description>&lt;P&gt;Here is an example you can try to get you started. It seems to do what you want, on the sample file you provided. In your particular case the custom parsing is relatively straightforward, although this isn't always the case. For deeper, more convoluted structures, parsing with recursion would likely be preferable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheers,&lt;/P&gt;
&lt;P&gt;Brady&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1);

aa = Load Text File(pickfile(), "JSON");

/* since only the "prediction" key of the main array produces varying row values, the following AA can be used	
	to write those columns with nonvarying values, after all else is done.
*/
constantsAA = remove(aa, "prediction");


dt = new table("Parsed results",
	&amp;lt;&amp;lt; new column("Points 1"), &amp;lt;&amp;lt;new column("Points 2"), &amp;lt;&amp;lt;new column("Points 3"), &amp;lt;&amp;lt;new column("Points 4"),
	&amp;lt;&amp;lt; new column("Cat Type", character),  &amp;lt;&amp;lt;new column("Score"), &amp;lt;&amp;lt;new column("Frame Type", character));
	 

// get a list of the possible cat types.
catList = aa["prediction"] &amp;lt;&amp;lt; get keys;

// for each, see if there is any data. If so, write it to the table
for each ({v,i}, catList, 
	if(nitems(aa["prediction"][v]) &amp;gt; 0,	//if the value for this cat type is an empty list, there is no data, so skip the body of the loop
		for each({w, k}, aa["prediction"][v],	//else, cycle through the list values, writing to the table
			dt &amp;lt;&amp;lt; add rows(1);
			dt[nrow(dt), 1::4]  = shape( Matrix(w["data"]["points"] ), 1);  // places the values from {{x, y}, {w, z}}, into the 1st 4 table columns
			dt:Cat type = v;
			dt:Score = w["score"];
			dt: Frame Type = w["type"];
			
		);
	)
);

//now, cycle through the AA containing "constant" data, adding this to the table
//Note: the char() function was employed to deal with the "flagged" column's unquoted 'true' value.
for each({v, i}, constantsAA &amp;lt;&amp;lt; get keys,
	dt &amp;lt;&amp;lt; new column(v, character, &amp;lt;&amp;lt;set each value(char(constantsAA[v])))
);

/*
There may remain some cleanup work to script: 
-- preferential reordering of columns
-- modification of data type for some of the constant-valued columns as required
-- possibly will have to recode "small cats" and "Small Cats" to be the same thing. Ideally this would be addressed before writing
	the original JSON file (i.e., at the time of collecting the data, however this was done)
*/&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 16 Aug 2022 16:23:23 GMT</pubDate>
    <dc:creator>brady_brady</dc:creator>
    <dc:date>2022-08-16T16:23:23Z</dc:date>
    <item>
      <title>How do you import JSONs that contain double nested arrays?</title>
      <link>https://community.jmp.com/t5/Discussions/How-do-you-import-JSONs-that-contain-double-nested-arrays/m-p/533809#M75656</link>
      <description>&lt;P&gt;I'm trying to import a number of JSON files that contain double nested arrays (see example below). The arrays don't follow a consistent format, there could be numerous objects in each array or none at all. I want to have one score per row as shown below.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="BagOfMiningLion_0-1660571634764.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/44785i17C260E0964527D6/image-size/medium?v=v2&amp;amp;px=400" role="button" title="BagOfMiningLion_0-1660571634764.png" alt="BagOfMiningLion_0-1660571634764.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="BagOfMiningLion_1-1660571678666.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/44786i4B2577BB0A52C537/image-size/medium?v=v2&amp;amp;px=400" role="button" title="BagOfMiningLion_1-1660571678666.png" alt="BagOfMiningLion_1-1660571678666.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;How can I get the data in this format using the import tool? Also, the name of the prediction is not always spelt the same e.g. sometimes it's "Small Cats" and sometimes it's "smallCats".&lt;/P&gt;</description>
      <pubDate>Sat, 10 Jun 2023 20:52:08 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-do-you-import-JSONs-that-contain-double-nested-arrays/m-p/533809#M75656</guid>
      <dc:creator>BagOfMiningLion</dc:creator>
      <dc:date>2023-06-10T20:52:08Z</dc:date>
    </item>
    <item>
      <title>Re: How do you import JSONs that contain double nested arrays?</title>
      <link>https://community.jmp.com/t5/Discussions/How-do-you-import-JSONs-that-contain-double-nested-arrays/m-p/533854#M75658</link>
      <description>&lt;P&gt;This is a lot easier to answer if you can share an example JSON file that you would like to import.&lt;/P&gt;</description>
      <pubDate>Mon, 15 Aug 2022 14:50:04 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-do-you-import-JSONs-that-contain-double-nested-arrays/m-p/533854#M75658</guid>
      <dc:creator>pauldeen</dc:creator>
      <dc:date>2022-08-15T14:50:04Z</dc:date>
    </item>
    <item>
      <title>Re: How do you import JSONs that contain double nested arrays?</title>
      <link>https://community.jmp.com/t5/Discussions/How-do-you-import-JSONs-that-contain-double-nested-arrays/m-p/534143#M75680</link>
      <description>&lt;P&gt;I attached below, thanks!&lt;/P&gt;</description>
      <pubDate>Tue, 16 Aug 2022 08:08:35 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-do-you-import-JSONs-that-contain-double-nested-arrays/m-p/534143#M75680</guid>
      <dc:creator>BagOfMiningLion</dc:creator>
      <dc:date>2022-08-16T08:08:35Z</dc:date>
    </item>
    <item>
      <title>Re: How do you import JSONs that contain double nested arrays?</title>
      <link>https://community.jmp.com/t5/Discussions/How-do-you-import-JSONs-that-contain-double-nested-arrays/m-p/534158#M75682</link>
      <description>&lt;P&gt;You might have to write custom parser for this. Use &lt;EM&gt;Load Text File(file_path, JSON);&lt;/EM&gt; and then parse the resulting Associative Array. It should be &lt;STRONG&gt;kinda &lt;/STRONG&gt;simple if the format &lt;STRONG&gt;is&lt;/STRONG&gt; consistent.&lt;/P&gt;</description>
      <pubDate>Tue, 16 Aug 2022 09:40:36 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-do-you-import-JSONs-that-contain-double-nested-arrays/m-p/534158#M75682</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2022-08-16T09:40:36Z</dc:date>
    </item>
    <item>
      <title>Re: How do you import JSONs that contain double nested arrays?</title>
      <link>https://community.jmp.com/t5/Discussions/How-do-you-import-JSONs-that-contain-double-nested-arrays/m-p/534314#M75687</link>
      <description>&lt;P&gt;To get you going on the custom parses: you should have a look at the stack mole:&amp;nbsp;&lt;A href="https://community.jmp.com/t5/JSL-Cookbook/The-stack-mole/ta-p/302563" target="_blank"&gt;https://community.jmp.com/t5/JSL-Cookbook/The-stack-mole/ta-p/302563&lt;/A&gt;&lt;/P&gt;&lt;P&gt;See attached implementation (requires your json file in the downloads folder). Not exactly what you want, but closer.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Aug 2022 14:16:28 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-do-you-import-JSONs-that-contain-double-nested-arrays/m-p/534314#M75687</guid>
      <dc:creator>pauldeen</dc:creator>
      <dc:date>2022-08-16T14:16:28Z</dc:date>
    </item>
    <item>
      <title>Re: How do you import JSONs that contain double nested arrays?</title>
      <link>https://community.jmp.com/t5/Discussions/How-do-you-import-JSONs-that-contain-double-nested-arrays/m-p/534353#M75694</link>
      <description>&lt;P&gt;Here is an example you can try to get you started. It seems to do what you want, on the sample file you provided. In your particular case the custom parsing is relatively straightforward, although this isn't always the case. For deeper, more convoluted structures, parsing with recursion would likely be preferable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheers,&lt;/P&gt;
&lt;P&gt;Brady&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1);

aa = Load Text File(pickfile(), "JSON");

/* since only the "prediction" key of the main array produces varying row values, the following AA can be used	
	to write those columns with nonvarying values, after all else is done.
*/
constantsAA = remove(aa, "prediction");


dt = new table("Parsed results",
	&amp;lt;&amp;lt; new column("Points 1"), &amp;lt;&amp;lt;new column("Points 2"), &amp;lt;&amp;lt;new column("Points 3"), &amp;lt;&amp;lt;new column("Points 4"),
	&amp;lt;&amp;lt; new column("Cat Type", character),  &amp;lt;&amp;lt;new column("Score"), &amp;lt;&amp;lt;new column("Frame Type", character));
	 

// get a list of the possible cat types.
catList = aa["prediction"] &amp;lt;&amp;lt; get keys;

// for each, see if there is any data. If so, write it to the table
for each ({v,i}, catList, 
	if(nitems(aa["prediction"][v]) &amp;gt; 0,	//if the value for this cat type is an empty list, there is no data, so skip the body of the loop
		for each({w, k}, aa["prediction"][v],	//else, cycle through the list values, writing to the table
			dt &amp;lt;&amp;lt; add rows(1);
			dt[nrow(dt), 1::4]  = shape( Matrix(w["data"]["points"] ), 1);  // places the values from {{x, y}, {w, z}}, into the 1st 4 table columns
			dt:Cat type = v;
			dt:Score = w["score"];
			dt: Frame Type = w["type"];
			
		);
	)
);

//now, cycle through the AA containing "constant" data, adding this to the table
//Note: the char() function was employed to deal with the "flagged" column's unquoted 'true' value.
for each({v, i}, constantsAA &amp;lt;&amp;lt; get keys,
	dt &amp;lt;&amp;lt; new column(v, character, &amp;lt;&amp;lt;set each value(char(constantsAA[v])))
);

/*
There may remain some cleanup work to script: 
-- preferential reordering of columns
-- modification of data type for some of the constant-valued columns as required
-- possibly will have to recode "small cats" and "Small Cats" to be the same thing. Ideally this would be addressed before writing
	the original JSON file (i.e., at the time of collecting the data, however this was done)
*/&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 16 Aug 2022 16:23:23 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-do-you-import-JSONs-that-contain-double-nested-arrays/m-p/534353#M75694</guid>
      <dc:creator>brady_brady</dc:creator>
      <dc:date>2022-08-16T16:23:23Z</dc:date>
    </item>
    <item>
      <title>Re: How do you import JSONs that contain double nested arrays?</title>
      <link>https://community.jmp.com/t5/Discussions/How-do-you-import-JSONs-that-contain-double-nested-arrays/m-p/534357#M75695</link>
      <description>&lt;P&gt;I had very similar version to &lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/3552"&gt;@brady_brady&lt;/a&gt;'s solution, but there are some small differences which might give some ideas to think about, so I'm sharing it:&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1);

file_path = "$DOWNLOADS\Test.json";
aa_all = Load Text File(file_path, JSON);

// separate prediction to it's own AA as it is only we need to loop over, rest are constants
aa_pred = Remove From(aa_all, "prediction");

// from each prediction, we need to collect:
	// data.points (multiple rows), score, type
// Note - we don't add points columns yet, because we don't how many we will have.
dt_pred = New Table("preds", 
	Add Rows(0),
	New Column("name", Character, Continuous),
	New Column("score", Numeric, Continuous),
	New Column("type", Character, Nominal)
);

// calculate max points and create points columns
max_points = 0;
For Each({{pred_key, pred_data}}, aa_pred,
	For Each({aa_data}, pred_data,
		If(Contains(aa_data, "data"),
			If(Contains(aa_data["data"], "points"),
				points_list = aa_data["data"]["points"];
				m_points = Matrix(points_list);
				new_max_points = N Rows(m_points) * N Cols(m_points);
				If(new_max_points &amp;gt; max_points,
					max_points = new_max_points;
				);
			);
		);
	);
);

point_cols = {};
For(i = 1, i &amp;lt;= max_points, i++,
	Insert Into(point_cols, dt_pred &amp;lt;&amp;lt; New Column("Points", Numeric, Continuous));
);
point_cols[1] &amp;lt;&amp;lt; Set Name("Points 1");

For Each({{pred_key, pred_data}}, aa_pred,
	If(N Items(pred_data) == 0,
		new_row = {};
		new_row = Eval List({pred_key, ., ""});
		Insert Into(new_row, AsList(J(1, max_points, .)`));
		dt_pred &amp;lt;&amp;lt; Add Rows(1);
		dt_pred[N Rows(dt_pred), 0] = new_row;
	,
		For Each({aa_data}, pred_data,
			new_row = Eval List({pred_key, aa_data["score"], aa_data["type"]});
			Insert Into(new_row, As List(Shape(Matrix(aa_data["data"]["points"]), max_points, 1)));
			dt_pred &amp;lt;&amp;lt; Add Rows(1);
			dt_pred[N Rows(dt_pred), 0] = new_row;
		);
	);
);

For Each({{key, val}}, aa_all,
	dt_pred &amp;lt;&amp;lt; New Column(key, Character, Nominal, &amp;lt;&amp;lt; Set Each Value(char(aa_all[key])));
);

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI-SPOILER&gt;</description>
      <pubDate>Tue, 16 Aug 2022 16:41:27 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-do-you-import-JSONs-that-contain-double-nested-arrays/m-p/534357#M75695</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2022-08-16T16:41:27Z</dc:date>
    </item>
  </channel>
</rss>

