Benjamin Crozat “Heard about Sevalla? They let you deploy PHP apps with ease.” Claim $50 →

1 minute read

Validate JSON in PHP with json_validate

Quick read from Ashley Allen on a handy PHP 8.3 feature: json_validate.

If you just need to check if a string is valid JSON, call the function and skip building arrays or objects. It is faster and uses less memory than json_decode when you do not need the data yet.

Example:

$json = '{ "name": "Jane","age": 28 }';

if (! json_validate($json)) {
    echo json_last_error_msg();
}

Ashley also compares json_validate to json_decode and reminds us not to parse twice. If you plan to use the data right away, json_decode is enough. If you only need to validate now and process later, json_validate is the better pick.

Good refresher for anyone handling large payloads, APIs, or user input in PHP.

Read more on ashallendesign.co.uk →


Did you like this article? Then, keep learning:

Help me reach more people by sharing this article on social media!

0 comments

Guest

Markdown is supported.

Hey, you need to sign in with your GitHub account to comment. Get started →

Great deals for developers

Search for posts and links

Try to type something…