cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
Choose Language Hide Translation Bar
EstelleS
Level III

How to set variables from imported json file / jmp Associative Array?

Hi All,

 

I am using Application Builder to develop a tool for data analytics with Python integration. I have created a button to export a user's parameter selections in case they want to re-upload those at a later time and perform the same data preprocessing. Currently, the way I export the parameters is I first script them to a csv (using Save Text File(...) ), then it can be opened as a jmp table and from there, the two columns (Variables and Values) can be set to an Associative Array and I also save that as a JSON file for use in a backend application. I have included a picture of what the exported jmp table might look like (for Variables 9 and 10, depending on the selection, there may be one, two, or three additional variables, so the table (and associative array/json) are not always the same size. Some of the values are strings and others are numbers.

 

My question is how can I best re-upload these parameters (preferably from both the jmp table and also from the JSON file) so that the variables are properly set to their values and can be used in later calculations. I have been playing around with For Each(...) from the Associative Arrays, but have not been able to set the variables properly. This is not the most straightforward way to save the parameters, but I was unable to figure out how to get the variables and values properly scripted to be sent directly to a jmp table. The current method I'm using to save parameters works for my purposes, but if another method or if if changing the structure/layout of the csv/table would make the uploading of the parameters easier, I am open to suggestions.

 

Thanks for any help.

2 ACCEPTED SOLUTIONS

Accepted Solutions
Craige_Hales
Super User

Re: How to set variables from imported json file / jmp Associative Array?

Try to avoid keeping multiple representations of the same data on disk. Since you have Python and JSON in the mix already, I'd recommend JSON as the disk format. Here are two examples; the first assumes you prefer the associative array in JMP and the second assumes a data table. I lean towards the first one because the names work better (below.)

// 1st example using Associative Array
variables = ["nThings" => 3,
"type" => "favs",
"things" => {"raindrops", "whiskers", "kettles"}];
filename = Save Text File( "$temp/variables.json", As JSON Expr( variables ) );
restoredVariables = Parse JSON( Load Text File( filename ) );
// this is not needed unless you want to compare the restored data to the original...
If( variables == restoredVariables,
    Write( "\!nGood restore\!n" ),
    Write( "\!nRestore does not match\!n" )
);
Show( variables, restoredVariables );


// 2nd example, using data table
dt = New Table( "variables",
    New Column( "key", "character", values( variables << getkeys ) ),
    New Column( "val", "expression", values( variables << getvalues ) )
);
dt << save( "$temp/table.json" );
dtRestored = Open( "$temp/table.json" );
dtRestored:val << datatype( "expression" );

// this is not needed, unless you want to compare the restored table to the original...
ok = (dt << Compare Data Tables( showwindow( 0 ), Unlink All, Link( {"key", "key"} ), Link( {"val", "val"} ) )) << Compare with( dtRestored );
Write( "\!n\!nData table on disk:\!n", Load Text File( "$temp/table.json" ) );
If( ok,
    Write( "\!ntables match" ),
    Write( "\!nhmmm, tables are different" )
);

with the associative array the lookups are simple:

type = restoredVariables["type"];

"favs"

 

 

Craige

View solution in original post

Craige_Hales
Super User

Re: How to set variables from imported json file / jmp Associative Array?

You might be able to use the default value for the associative array, or you might explicitly test if the key exists before using it.

See @jthi  comments in Associative Array: Try(), Contains() or Default Value?  .  Is the set of keys small enough you can list all possible combinations? Or is it a set that can't be predicted and you need to make new variables from?

Craige

View solution in original post

5 REPLIES 5
EstelleS
Level III

Re: How to set variables from imported json file / jmp Associative Array?

Here is a very simple example to illustrate what I am trying to do (for the Associative Array case, not the .json file case):

 

AA = Associative Array();
AA["One"] = {1, 2, 3};
AA["Two"] = {"a", "b", "c"};
key_list = AA << Get Keys();
val_list = AA << Get Values();

For(i=1, i<=N Items(key_list), i++,
	key_list[i] = val_list[i];
	Show(key_list[i]);
);

 

The log output for this is:

// key_list[i] = {1, 2, 3};
// key_list[i] = {"a", "b", "c"};

Where my desired outcome is (so these variables can be sent to Python with Python Send(...) and used there):

// One = {1, 2, 3};
// Two = {"a", "b", "c"};

I feel like this is very close to working the way I would like, but I am having no such luck!

 

Thanks!

Craige_Hales
Super User

Re: How to set variables from imported json file / jmp Associative Array?

Try to avoid keeping multiple representations of the same data on disk. Since you have Python and JSON in the mix already, I'd recommend JSON as the disk format. Here are two examples; the first assumes you prefer the associative array in JMP and the second assumes a data table. I lean towards the first one because the names work better (below.)

// 1st example using Associative Array
variables = ["nThings" => 3,
"type" => "favs",
"things" => {"raindrops", "whiskers", "kettles"}];
filename = Save Text File( "$temp/variables.json", As JSON Expr( variables ) );
restoredVariables = Parse JSON( Load Text File( filename ) );
// this is not needed unless you want to compare the restored data to the original...
If( variables == restoredVariables,
    Write( "\!nGood restore\!n" ),
    Write( "\!nRestore does not match\!n" )
);
Show( variables, restoredVariables );


// 2nd example, using data table
dt = New Table( "variables",
    New Column( "key", "character", values( variables << getkeys ) ),
    New Column( "val", "expression", values( variables << getvalues ) )
);
dt << save( "$temp/table.json" );
dtRestored = Open( "$temp/table.json" );
dtRestored:val << datatype( "expression" );

// this is not needed, unless you want to compare the restored table to the original...
ok = (dt << Compare Data Tables( showwindow( 0 ), Unlink All, Link( {"key", "key"} ), Link( {"val", "val"} ) )) << Compare with( dtRestored );
Write( "\!n\!nData table on disk:\!n", Load Text File( "$temp/table.json" ) );
If( ok,
    Write( "\!ntables match" ),
    Write( "\!nhmmm, tables are different" )
);

with the associative array the lookups are simple:

type = restoredVariables["type"];

"favs"

 

 

Craige
EstelleS
Level III

Re: How to set variables from imported json file / jmp Associative Array?

Thanks for the reply! I figured the Associative Array lookups were pretty simple and I just wasn't trying the correct syntax.

 

I do still have a problem, however... For two of my variables, there are several different options (variable1 could be named "var1option1", "var1option2", etc...) and all of the different options each have a possible one, two, or three additional variables specific to the option choice. All of these variable names are different. Is there a way to unload these parameters without knowing beforehand what the selections are?

 

Like in this scenario, either "var1option1" would exist or "var1option2"... not both, but there is no way for me to know beforehand and this script would have to work for all possible scenarios. Is there a way to avoid errors when the variable name does not exist in the Associative Array? It seems I would have to use If statements?

var1option1 = restoredVariables["var1option1"]; 
var1option1sub1 = restoredVariables["var1option1sub1"]; 
var1option1sub2 = restoredVariables["var1option1sub2"]; 
var1option1sub3 = restoredVariables["var1option1sub3"]; 

var1option2 = restoredVariables["var1option2"];
var1option2sub1 = restoredVariables["var1option2sub1"]; 
var1option2sub2 = restoredVariables["var1option2sub2"]; 

Thanks again!

Craige_Hales
Super User

Re: How to set variables from imported json file / jmp Associative Array?

You might be able to use the default value for the associative array, or you might explicitly test if the key exists before using it.

See @jthi  comments in Associative Array: Try(), Contains() or Default Value?  .  Is the set of keys small enough you can list all possible combinations? Or is it a set that can't be predicted and you need to make new variables from?

Craige
EstelleS
Level III

Re: How to set variables from imported json file / jmp Associative Array?

Thanks again, @Craige_Hales ! That is a helpful post. Although it may be slightly bulky, I should be able to list all combinations of keys and use those methods to get it to work.