cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar

JSL to JSON

With the latest JMP 14 JSON Features: Parse JSON(); JSON To Data Table(); and JSON To List(); it would be really great if we could reverse this process.  I would like to turn a JSL Associative Array into a JSON string.  I realize there's a work around to turn a Data Table into a JSON string, but the most important of these features would be turning an AA into a JSON string.

8 Comments
erin_vang
Level II
Strong agree! I just sent much the same request to tech support: Hi. I’d like to request a feature analogous to the existing JSL function: dt= JSON To Data Table(); While it’s not complicated to script it manually, it would be nicer (and tidier) if we could also have one of these to do the reverse: dt << get JSON(); json = JSON From Data Table( dt ); I think the first form would be the most natural for experienced JSL users, but the second form surfaces it in the JSL lexicon a little better. Either way would work, of course. Similarly, while not too tough to do this manually, it would be nice to have an inverse for this: array|list = Parse JSON( json ); such as: json = As JSON ( list|array ); Thanks for considering these!
msharp
Super User (Alumni)

As I mention, for data tables there is an easy work around, which I'm not sure you are aware of.  It's still convoluted and should be a simple one liner.  For list and array's there is no work around however.  You'll have to script some solution.  I do hope JMP includes this directly in JMP.

 

Names Default To Here( 1 );
tempdir = "C:\TEMP\bigclass.json";
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
dt << Save( tempdir );
json = load text file( tempdir );
Delete File( tempdir );
dt2 = JSON To Data Table( json );
erin_vang
Level II
Thanks, msharp, but what you're describing is what's already there: from JSON into data table. What we both requested is the reverse: from data table into JSON. Happily, it does exist already, though, and I've just bumped into where they've hidden it. In the GUI, it is in File/Export/JSON data, and the JSL equivalent is dt << save as(“filename.json”).
msharp
Super User (Alumni)

...

@erin_vang @msharp ,

As I think you both have figured out, dt << save [as] ("something.json") will create a json string output you want. I have passed the request along to our developers for a JSL message to retrieve the JSON string directly form the table without the need to save and reload from a file. 

However, the part that already does exist in JMP is the conversion from a JSL list/associative array to JSON. This can be done using the As JSON Expr() function.

aa = ["age" => 17, "height" => 68, "name" => "KIRK", "sex" => "M", "weight" => 134];

json = As JSON Expr( aa );

Write( "\!N" || json );

Log:

{"age":17,"height":68,"name":"KIRK","sex":"M","weight":134}

Regards,

Justin

liqinglei419
Level II

Hi @erin_vang, I had this issue as well, where one couldn't convert a table to a JSON format. I ended up making a script that does that. It isn't the most elegant, but it definitely does do the job quite quickly!

 

//initialize with an empty list, and create list with i rows
dt=Current Data Table();
aa_list=list();
for(i=1,i<=nrows(dt),i+=1,
	aa_list[i]=associative array();
);
//Each dictionary/AA is a row, insert each row's column value to the dictionary/AA
for(j=1,j<=N items(aa_list),j+=1,
	for(m=1,m<=ncols(dt),m+=1,
		aa_list[j]<<Insert(char(column name(m)),dt[j,m]);
	);
);

json=As JSON Expr(aa_list);

Hope that helps until something formal is developed!

Status changed to: Acknowledged

Hi @msharp, thank you for your suggestion! We have captured your request and will take it under consideration.

hardner
Level V

Would be nice if above example of asJSONexpr() acting on an associative array was an example in the scripting index for that function. Ditto for .json being listed among the "supported formats include" in the description for SaveAs.