BookmarkSubscribeSubscribe to RSS Feed

Define JSON structure for data exchange which supports table & column properties

Highlighted
vt_sailor

Community Trekker

Joined:

May 11, 2017

Good Morning,

 

I'm working on a standard data exchange structure to allow JMP and our other applications to exchange data.  JMP's native JSON capability handles the data fine, but offers no options for saving properties.  I'm wondering if anyone has already gone through this exercise and would be willing to share to you came up with?

 

Thank you

Ray Bunkofske

4 REPLIES
acosand

New Contributor

Joined:

Jan 22, 2018

Hi Ray,
I put this together for saving some data out to JSON. No guarantees, but you're welcome to give it a shot.
Cheers
-andrew
// Serialize Associative Arrays, Lists, or nestings of them to JSON (http://www.json.org/)
// Iterate through Associative Array keys or List, recurse on values.
// Use "Local" and "Recurse" to isolates scopes.
// https://www.jmp.com/support/help/13-2/Recursion.shtml
// http://www.jmp.com/support/help/Global_and_Local_Variables.shtml
toJSON = Function( {input, depth=0, maxdepth=16},
	Local( {output, current_item},
		if (
			depth > maxdepth,
				Throw("toJSON recursion depth " || depth || " exceeds limit of " || maxdepth),
			Is List(input),
				output = "[";
				current_item = input << First;
				While (Is Empty(current_item) == 0,
					output = output || Recurse(current_item, depth+1);
					current_item = input << Next(current_item);
					If (Is Empty(current_item) == 0, output = output || ",";);
				);
				output =  output || "]",
			Is Associative Array(input),
				output = "{";
				current_item = input << First;
				While (Is Empty(current_item) == 0,
					output = output || "\!"" || Char(current_item) || "\!":" || Recurse(input[current_item], depth+1);
					current_item = input << Next(current_item);
					If (Is Empty(current_item) == 0, output = output || ",";);
				);
				output =  output || "}",
			Is Number(input),
				output = Char(input),
			Uppercase(input) == "FALSE",
				output = "false",
			Uppercase(input) == "TRUE",
				output = "true",
			// else
				output = "\!"" || Char(input) || "\!"",
		);
		// return:
		output;
	);
);
acosand

New Contributor

Joined:

Jan 22, 2018

Already found one problem with that; I was trying to iterate through lists as if they were associative arrays. This should work better.
// Serialize Associative Arrays, Lists, or nestings of them for JSON export (http://www.json.org/)
// Iterate through Associative Array keys or List, recurse on values.
// Use "Local" and "Recurse" to isolates scopes.
// https://www.jmp.com/support/help/13-2/Recursion.shtml
// http://www.jmp.com/support/help/Global_and_Local_Variables.shtml
// TODO: consider pretty formatting
// TODO: consider opiton to include/exclude empty Lists and AAs.
toJSON = Function( {input, depth=0, maxdepth=16},
	Local( {output, current_item, num_list_items, list_item_count},
		if (
			depth > maxdepth,
				Throw("toJSON recursion depth " || Char(depth) || " exceeds limit of " || Char(maxdepth)),
			Is List(input),
				output = "[";
				num_list_items = N Items(input);
				list_item_count = 0;
				While (num_list_items > list_item_count,
					list_item_count++;
					output ||= Recurse(input[list_item_count], depth+1);
					If (num_list_items > list_item_count, output ||= ",";);
				);
				output ||= "]",
			Is Associative Array(input),
				output = "{";
				current_item = input << First;
				// TODO: check value for Is Missing before adding key
				While (Is Empty(current_item) == 0,
					output ||= "\!"" || Char(current_item) || "\!":" || Recurse(input[current_item], depth+1);
					current_item = input << Next(current_item);
					If (Is Empty(current_item) == 0, output ||= ",";);
				);
				output ||= "}",
			Is Number(input),
				output = Char(input),
			Uppercase(input) == "FALSE",
				output = "false",
			Uppercase(input) == "TRUE",
				output = "true",
			// else
				output = "\!"" || Char(input) || "\!"",
		);
		// return:
		output;
	);
);
vt_sailor

Community Trekker

Joined:

May 11, 2017

Andrew,

Thank you, I very much appreciate the effort involved here.  To use this do I pass in individual elements and get them returned in a JSON structure?  

 

Thanks again

acosand

New Contributor

Joined:

Jan 22, 2018

In my case I have an Associateve Array which contains values and other AAs and lists nested inside it, and I need to send that to another system in a JSON file.  If you can get the properties you want as a List or AA you could probably pass it into this funciton, or you may be able to adapt it to iterate over other structures.