In recent project I needed to import a list of things coming from an API call into HTML / javascript elements, so I developed a small plugin to translate a thing or a list of things into a JSON. I decided to polish it up and share it with others. The name of the plugin is in plural since there might be some other tools in it in the future
The plugin has an element GetJSON which converts a thing into a JSON string resulting in {āfieldNameā:āvalueā, ā¦} structure. It converts a list of things into an array of objects of the same structure, i. e. [{āfieldName0ā:āvalue0ā,āfieldName1ā:āvalue1ā, ā¦},{ā¦}, ā¦].
Usage:
- put the element on the page or any grouping element
- set the DataType property to the data type of the thing(s) to convert
- set the data source in appropriate property - a Thing to convert or a List to convert.
If you set both the thing and the list source, the JSON of the list will be returned.
The returned JSON is sanitized, so it should be safe to transfer data including backslashes, quotation marks etc.
In HTML or javascript element you use the syntax:
<script>
var myStr = "GetJSON Name's value";
var myThing = JSON.parse(myStr);
alert(myThing.myFieldName);
</script>
to get the value of the fieldname of a thing, or
<script>
var myStr = "GetJSON Name's value";
var myThings = JSON.parse(myStr);
alert(myThings[0].myFieldName);
</script>
to get the value of the fieldname of the first thing in the list.
Plugin doesnāt recurse on the lists within fields, but if somebody needs this and the time will permit, it can be done.