%ToJSON

This special method converts an internal object to a JSON string.

All special method names and keywords are case-insensitive.

Syntax

object->%ToJSON({indent})

Syntax elements

objectAn expression that evaluates to an internal object.

indentThe separation of elements of the string:

0No separation (default).

1Inserts a single space between JSON elements.

2 or morePretty printing — Inserts new lines where appropriate and indents nested elements by multiples of the indent value.

Applicability

Internal objects only.

Examples

Given:

Dylan = %New()
Dylan->firstName = "Bob"
Dylan->lastName = "Dylan"
Dylan->dateOfBirth = 1941

Example 1

Converts the Dylan object to a compact JSON string with no spacing, suitable for efficient inter-process communication.

PRINT Dylan->%ToJSON()

which produces:

{"firstName":"Bob","lastName":"Dylan","dateOfBirth":1941}

Example 2

Converts Dylan to a JSON string with a single space between elements.

PRINT Dylan->%ToJSON(1)

Produces:

{ "firstName": "Bob", "lastName": "Dylan", "dateOfBirth": 1941 }

See also

%New, %ToMV, %ToXML