Skip to main content
Use the 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.
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.
... | eval json_output = tojson({"key": "value"})
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.
SELECT JSON_OBJECT('key' VALUE 'value') AS json_output FROM dual;

Usage

Syntax

dynamic_to_json(dynamic)

Parameters

NameTypeDescription
dynamicdynamicThe 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 applying tostring() 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

Convert a dynamic object containing request metadata to a JSON string for logging or export purposes.Query
['sample-http-logs']
| extend metadata = bag_pack('method', method, 'status', status, 'duration', req_duration_ms)
| extend json_metadata = dynamic_to_json(metadata)
| project _time, ['uri'], json_metadata
Run in PlaygroundOutput
_timeurijson_metadata
Jun 24, 09:28:10/api/users{“duration”:150,“method”:“GET”,“status”:“200”}
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.
  • todynamic: Converts a JSON string to a dynamic value. Use todynamic to parse JSON strings, and dynamic_to_json to serialize dynamic values back to strings.
  • tostring: Converts any scalar value to a string. Use tostring for simple scalar conversions, and dynamic_to_json when you need canonical JSON formatting for dynamic values.
  • parse_json: Parses a JSON string into a dynamic value. Use parse_json to create dynamic values from JSON strings, and dynamic_to_json to convert them back.
  • toarray: Converts a dynamic value to an array. Use toarray when you need array operations, and dynamic_to_json when you need string output.