How to check is a string valid json in PHP ?

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate & JSON is Language independent.

PHP has 4 built in JSON functions called json_encode(), json_decode(),json_​last_​error_​msg(),json_​last_​error().

json_encode() & json_decode() : This function are used for encoding and decoding in JSON format.

json_​last_​error_​msg() & json_​last_​error() : This function are use full after json_encode() or json_decode() call. json_​last_​error_​msg() function returns the error string , json_​last_​error() Returns an integer which can be found in the PHP Manual. for no error checking we can compare with JSON_ERROR_NONE constant.

Json Encode:

Json Decode:

Check for valid JSON

we can use json_last_error() PHP function to check validity of JSON, It returns JSON_ERROR_NONE predefined constant value if JSON successfully decoded or encoded by PHP’s josn functions.

How to use is_json function:

That’s all for today I hope you like this tutorial please don’t forget to give me your feedback.

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

0 0 vote
Article Rating
Notify of
Inline Feedbacks
View all comments Protection Status
Would love your thoughts, please comment.x