WP_CLI\Utils

is_json()WP-CLI 1.0

Check whether a given string is a valid JSON representation.

No Hooks.

Return

true|false. Whether the provided string is a valid JSON representation.

Usage

is_json( $argument, $ignore_scalars );
$argument(string) (required)
String to evaluate.
$ignore_scalars(true|false)
Whether to ignore scalar values.
Default: true

is_json() code WP-CLI 2.8.0-alpha

function is_json( $argument, $ignore_scalars = true ) {
	if ( ! is_string( $argument ) || '' === $argument ) {
		return false;
	}

	if ( $ignore_scalars && ! in_array( $argument[0], [ '{', '[' ], true ) ) {
		return false;
	}

	json_decode( $argument, $assoc = true );

	return json_last_error() === JSON_ERROR_NONE;
}