Skip to content

json.toHtmlTable function#5934

Open
Baaaaaz wants to merge 11 commits intoRPTools:developfrom
Baaaaaz:feature/5906-new-json-html-functions
Open

json.toHtmlTable function#5934
Baaaaaz wants to merge 11 commits intoRPTools:developfrom
Baaaaaz:feature/5906-new-json-html-functions

Conversation

@Baaaaaz
Copy link
Contributor

@Baaaaaz Baaaaaz commented Jan 29, 2026

Requirements for Contributing a Bug Fix or Enhancement

closes #5906

Description of the Change

New MTScript function json.toHtmlTable(json [, options]) which recursively transforms json into nested html tables with applied options.

Possible Drawbacks

None foreseen, but added experimental flag while it matures.

Documentation Notes

To be done, but options are (in no particular order):

  • LeadObjectKeys - Return a specific order of object keys (if present) to start with
  • RearObjectKeys - Return a specific order of object keys (if present) to end with
  • SortObjectKeys - Return object keys in a sorted order (for keys not defined as lead and rear object keys)
  • Attributes - Sets any html attributes on the root table
  • Caption - Adds a caption element to the root table
  • Collapsible - Whether to output html tables with a detail/summary wrapper
  • CollapsibleOpenDepth - Whether a detail/summary wrapper is open by default
  • Titles - Whether to output the json path as html title attributes
  • ArrayOfObjects - Whether to pivot json objects within a json array
  • ObjectOfObjects - Whether to pivot json objects within a json object
  • AssetImage - Whether json values starting with "assetid://" should returned as an html <img> element
  • EscapeHtml - Whether json values containing certain html characters in should be escaped.
  • SanitizeHtml - Whether to sanitize the html returned from jsonToHtmlTable.
  • Input - Whether json values should be converted to input elements for use in a form (defaults to false)

Release Notes

Add new MTScript function json.toHtmlTable()


This change is Reviewable

@github-actions github-actions bot added the feature Adding functionality that adds value label Jan 29, 2026
@Baaaaaz
Copy link
Contributor Author

Baaaaaz commented Jan 29, 2026

An example MTScript is:

[h: vJson = getInfo("map")]

[h: vOptions = json.set("{}", 
	"caption", "Example Data",
	"attributes", json.set("{}", "id","exampleId", "class","exampleClass"),
	"collapsible", 1,
	"collapsibleOpenDepth", 2,
	"escapeHtml", 0,
	"sanitizeHtml", 1,
	"assetImage", 1,
	"arrayOfObjects", 1,
	"objectOfObjects", 1,
	"leadObjectKeys", json.append("", "id", "name", "level"),
	"sortObjectKeys", "asc",
	"rearObjectKeys", json.append("", "source", "page"),
	"input", 0
)]

[frame5("INFO"): {
	<html>
		<head>
			<link rel='stylesheet' type='text/css' href='lib://JsonHtmlTable/macro/style.css?cachelib=false'>
			<style>
				[r:'
					span.json-array, th.json-array {background-color: LightSkyBlue;}
					span.json-object, th.json-object {background-color: DarkSeaGreen;}
					[data-json-key="name"] {color: red}
					[data-json-index="1"] {background-color: yellow}
				']
				</style>
		</head>
		
		<body>
			[r: json.toHtmlTable(vOptions, json.set("{}", "caption", "json.toHtmlTable Options"))]
			<hr>
			[r: json.toHtmlTable(vJson, vOptions)]
			<hr>
			[r: replace(json.toHtmlTable(vJson, vOptions), "<", "&lt;")]			
		</body>
	</html>
}]
image

@Baaaaaz
Copy link
Contributor Author

Baaaaaz commented Jan 29, 2026

Or another example using jmrs 5E spell library json:
image

@Baaaaaz Baaaaaz marked this pull request as draft February 7, 2026 18:21
@Baaaaaz Baaaaaz changed the title json.html functions json.toHtmlTable function Feb 13, 2026
@Baaaaaz Baaaaaz marked this pull request as ready for review February 13, 2026 08:05
@Baaaaaz
Copy link
Contributor Author

Baaaaaz commented Feb 13, 2026

Ready for a review :)


public class JsonHtmlFunctions {

JsonHtmlFunctions(JsonMTSTypeConversion converter) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a @nonnull annotation to the argument to help IDEs pick up potential errors

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}

/** Track the current depth in the json hierarchy during the conversion to html table(s) */
private Integer jsonPathDepth;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you are not using null as a "not defined" value this should be just an int to avoid all the auto boxing/unboxking every time you use it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

optionSanitizeHtml =
options.has("sanitizehtml")
? !options.get("sanitizehtml").getAsBigDecimal().equals(BigDecimal.ZERO)
: true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because all of this is user input you need to check they passed the correct type and raise a meaningfull error if they didn't, rather than the JSON library throwing a vague error they would not be able to debug.

Especially since the problems with MT Json not being able to use booleans have been addressed many versions ago, I would expect to be able to use boolean true/false instead of 1/0 in the options.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can take either boolean, or 1/0. Also added some option value validation and relevant exception messages.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature Adding functionality that adds value

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: A new jsonToHtmlTable function

2 participants