diff --git src/wp-includes/functions.php src/wp-includes/functions.php
index 204211b..0f52595 100644
--- src/wp-includes/functions.php
+++ src/wp-includes/functions.php
@@ -2871,6 +2871,9 @@ function _scalar_wp_die_handler( $message = '' ) {
  * @return string|false The JSON encoded string, or false if it cannot be encoded.
  */
 function wp_json_encode( $data, $options = 0, $depth = 512 ) {
+	// Prepare the data for JSON serialization.
+	$data = _wp_json_prepare_data( $data );
+
 	/*
 	 * json_encode() has had extra params added over the years.
 	 * $options was added in 5.3, and $depth in 5.5.
@@ -2884,9 +2887,6 @@ function wp_json_encode( $data, $options = 0, $depth = 512 ) {
 		$args = array( $data );
 	}
 
-	// Prepare the data for JSON serialization.
-	$args[0] = _wp_json_prepare_data( $data );
-
 	$json = @call_user_func_array( 'json_encode', $args );
 
 	// If json_encode() was successful, no need to do more sanity checking.
@@ -2897,7 +2897,7 @@ function wp_json_encode( $data, $options = 0, $depth = 512 ) {
 	}
 
 	try {
-		$args[0] = _wp_json_sanity_check( $data, $depth );
+		$args[0] = _wp_json_sanity_check( $args[0], $depth );
 	} catch ( Exception $e ) {
 		return false;
 	}
