json_validate
json_validate(string $json, int $depth = 512, int $flags = 0): bool
Checks whether a string is syntactically valid JSON (PHP 8.3+, without building the value).
C C23
#include <stdio.h>
#include "cJSON.h"
static int json_validate(const char *s) {
cJSON *v = cJSON_Parse(s); // a parse attempt doubles as validation (NULL on failure)
if (v == NULL) return 0;
cJSON_Delete(v);
return 1;
}
int main(void) {
printf("%s\n", json_validate("{\"a\":1}") ? "true" : "false");
printf("%s\n", json_validate("{bad") ? "true" : "false");
return 0;
}
true false
C++ C++26
#include <iostream>
#include "nlohmann/json.hpp"
int main() {
// json::accept validates without building the DOM
std::cout << (nlohmann::json::accept("{\"a\":1}") ? "true" : "false") << "\n";
std::cout << (nlohmann::json::accept("{bad") ? "true" : "false") << "\n";
return 0;
}
true false
C# v11.0.100
bool JsonValidate(string s)
{
try
{
System.Text.Json.JsonDocument.Parse(s); // parse as validation; TryParse-style APIs are newer
return true;
}
catch (System.Text.Json.JsonException)
{
return false;
}
}
Console.WriteLine(JsonValidate("{\"a\":1}") ? "true" : "false");
Console.WriteLine(JsonValidate("{bad") ? "true" : "false");
true false
Go v1.27.4
package main
import (
"encoding/json"
"fmt"
)
func main() {
fmt.Println(json.Valid([]byte(`{"a":1}`)))
fmt.Println(json.Valid([]byte(`{bad`)))
}
true false
Java v27.0.1
import org.json.JSONException;
import org.json.JSONTokener;
class Main {
static boolean jsonValidate(String s) {
try {
new JSONTokener(s).nextValue();
return true;
} catch (JSONException e) {
return false;
}
}
public static void main(String[] args) {
System.out.println(jsonValidate("{\"a\":1}"));
System.out.println(jsonValidate("{bad"));
}
}
true false
JavaScript v26.13.1
function jsonValidate(s) {
try {
JSON.parse(s);
return true;
} catch (e) {
return false;
}
}
console.log(jsonValidate('{"a":1}'));
console.log(jsonValidate("{bad"));
true false
PHP v8.6.0
var_dump(json_validate('{"a":1}'));
var_dump(json_validate("{bad"));
bool(true) bool(false)
Python v3.15.2
import json
def json_validate(s):
try:
json.loads(s)
return True
except json.JSONDecodeError:
return False
print(json_validate('{"a":1}'))
print(json_validate("{bad"))
True False
Rust v1.99.1
fn main() {
// from_str::<Value> succeeds only for syntactically valid JSON (validation without a model type)
println!("{}", serde_json::from_str::<serde_json::Value>("{\"a\":1}").is_ok());
println!("{}", serde_json::from_str::<serde_json::Value>("{bad").is_ok());
}
true false
TypeScript v26.13.1
function jsonValidate(s: string): boolean {
try {
JSON.parse(s);
return true;
} catch {
return false;
}
}
console.log(jsonValidate('{"a":1}'));
console.log(jsonValidate("{bad"));
true false
Language differences
C: uses cJSON's cJSON_Parse (NULL on failure, remember cJSON_Delete).
C++: nlohmann likewise offers json::accept.
C#: JsonDocument.Parse catching JsonException.
Go: the only one with a purpose-built builtin — json.Valid.
Java: the standard library has no JSON — the example uses org.json's JSONTokener.nextValue catching JSONException (note org.json is lenient about unquoted keys, so its verdict can differ from the strict standard).
JavaScript: JSON.parse catching SyntaxError.
PHP: PHP 8.3 introduced json_validate — syntax checking without building the value, cheaper in memory than json_decode + json_last_error (decoding a large document just to judge validity is wasteful); it takes a depth argument to cap nesting, like json_decode.
Python: the idiom is parse + catch — json.loads catching JSONDecodeError.
Rust: serde_json::from_str::<Value> with is_ok() (parsed into the generic Value, then discarded).
TypeScript: same as JS — JSON.parse catching SyntaxError.