dynamic_to_json function to convert a dynamic-typed value—such as a property bag, array, or nested JSON structure—into a canonical JSON string representation. This is helpful when you want to serialize dynamic data for storage, transmission, or further string manipulation.
You typically use dynamic_to_json when working with semi-structured data that you need to convert to a string format, especially when exporting data or passing dynamic values to functions that expect string input.
For users of other query languages
If you come from other query languages, this section explains how to adjust your existing queries to achieve the same results in APL.Splunk SPL users
Splunk SPL users
In Splunk, you typically use
tojson or mvjoin with JSON formatting to convert structured data to JSON strings. In APL, dynamic_to_json provides a similar conversion from dynamic values to canonical JSON strings.ANSI SQL users
ANSI SQL users
In standard SQL, you use
JSON_OBJECT or JSON_ARRAY functions to create JSON strings, or CAST(... AS JSON) to convert values. In APL, dynamic_to_json converts dynamic values (which can be created from JSON strings using todynamic) back into canonical JSON string representations.Usage
Syntax
Parameters
| Name | Type | Description |
|---|---|---|
| dynamic | dynamic | The dynamic value to convert to a JSON string. |
Returns
Returns a canonical JSON string representation of the input according to the following rules:- If the input is a scalar value of type other than
dynamic, the output is the result of applyingtostring()to that value. - If the input is an array of values, the output is composed of the characters
[,,, and]interspersed with the canonical representation of each array element. - If the input is a property bag, the output is composed of the characters
{,,, and}interspersed with colon (:)-delimited name/value pairs of the properties. The pairs are sorted by the names, and the values are in the canonical representation described here.
Use case examples
- Log analysis
- OpenTelemetry traces
- Security logs
Convert a dynamic object containing request metadata to a JSON string for logging or export purposes.QueryRun in PlaygroundOutput
This example creates a dynamic object from log fields and converts it to a JSON string, which you can use for exporting structured data or passing to string manipulation functions.
| _time | uri | json_metadata |
|---|---|---|
| Jun 24, 09:28:10 | /api/users | {“duration”:150,“method”:“GET”,“status”:“200”} |
List of related functions
- todynamic: Converts a JSON string to a dynamic value. Use
todynamicto parse JSON strings, anddynamic_to_jsonto serialize dynamic values back to strings. - tostring: Converts any scalar value to a string. Use
tostringfor simple scalar conversions, anddynamic_to_jsonwhen you need canonical JSON formatting for dynamic values. - parse_json: Parses a JSON string into a dynamic value. Use
parse_jsonto create dynamic values from JSON strings, anddynamic_to_jsonto convert them back. - toarray: Converts a dynamic value to an array. Use
toarraywhen you need array operations, anddynamic_to_jsonwhen you need string output.