Outgoing Webhooks: JSON format – Type casting the values into the correct format

Copy and paste the following snippet into your child theme’s functions.php to automatically typecast values. Feel free to modify the code as needed.

add_filter('automator_send_webhook_data_format', function( $fields, $original, $data_type ) {

	$fields_arr = json_decode( $fields, true );
        // Add your the field value to cast.
	$casted = array(
		'true' => true,
		'false' => false,
		'null' => null,
		'undefined' => null
	);

	$casted_fields = array();

	if ( ! empty( $fields_arr ) ) {

		foreach( $fields_arr as $field_key => $field_val ) {

			$value = $field_val;

			if ( in_array( $field_val, array_keys( $casted ) ) ) {
				$value = $casted[$field_val];				
			}

			// Example, cast numeric to integer.
			if ( is_numeric( $field_val ) ) {
				$value = intval( $field_val );
			}

			$casted_fields[$field_key] = $value;
		}
		
	}

	if ( ! empty( $casted_fields ) ) {
		return wp_json_encode( $casted_fields );
	}
	
	return $fields;

}, 10, 3 );