JsonSerialize
Turns a simple type, Properties object, or dynamic object into a JSON string.
Syntax
JsonSerialize(<value>[, <indent>[, <single_line_arrays>[, <categoricals_as_text>]]]) As String
Parameters
<value>
Type: Variant
The value to be serialized into JSON. Simple types such as integer or text values and objects can be serialized into JSON. Objects returned by CreateObject() used without a parameter, or Properties collections (mrProperties.Properties) can be serialized. Attempts to serialize other object types result in an error.
<indent>
Type: Long
(Optional.) Formatting option that specifies the number of spaces when the JSON is formatted over multiple lines. If the value is negative or omitted, all JSON is formatted on a single line.
<single_line_arrays>
Type: Boolean
(Optional.) False formats arrays over multiple lines. True formats the array elements on a single line.
If multi-line formatting is enabled (that is, Indent is zero or greater than zero), the default value is to format arrays over multiple lines.
If multi-line formatting is not enabled (that is, Indent is omitted or less than zero), this value is ignored.
<categoricals_as_text>
Type: Boolean
(Optional.) Specifies formatting for categorical values. By default categorical values are formatted as an array, for example [32,33].
If <categoricals_as_text> is True, the categorical value is formatted as a string, for example: {32,33}.
If a metadata document is available, categorical names are used, for example: {north,south}.
(return)
Type: String
Returns the variant in JSON format in a string.
Notes
▪There are several ways to access values on the dynamic object returned from JsonSerialize. For example:
Debug.Log("Logo: " + JsonObject.Logo)
Debug.Log("Logo: " + JsonObject["Logo"])
Debug.Log("Logo: " + JsonObject._Properties["Logo"])
▪Arrays defined in JSON are returned as categorical values by the dynamic object. Use the For Each statement to iterate over the items in value. For example:
Dim Value, Index
Index = 0
For Each Value in JsonObject.Values
Debug.Log("Values[" + CText(Index) + "] = " + CText(Value))
Index = Index + 1
Next
▪Use VarType to test the type of a value returned by property. For example:
VarType = VarType(PropertyValue)
If VarType = mr.Object Then
' The value is an object
' ...
ElseIf VarType = mr.Categorical Then
' The value is an array
' ...
End If
Examples
The following example uses CreateObject() to create a dynamic object, and shows how to use the . (dot) notation to specify property names.
Dim StyleObject
Dim JSON
Dim FSO
Dim TextStream
Dim Values[2]
Set StyleObject = CreateObject()
StyleObject.TemplateName = "PeopleWorking"
StyleObject.Color = "Red"
StyleObject.Logo = "PeopleWorkingRed.png"
StyleObject.Categories = {23}
Values[0] = "Hello"
Values[1] = 42.01
StyleObject.Values = Values
StyleObject.Font = CreateObject()
StyleObject.Font.Face = "Arial"
StyleObject.Font.Size = 12
StyleObject.Font.Italics = True
JSON = JsonSerialize(StyleObject, 4, True)
Debug.Log(JSON)
The following example serializes a categorical value:
Routing(Web)
Location.Ask()
TextQ.Response.Value = JsonSerialize(Location.Response.Value, -1, False, True)
TextQ.Show()
End Routing
See also