LMSouq
php-dev Open

How to test if a string is JSON or not?

JE
jeffery_the_wind
1 month ago
3 views
Problem Description
I have a simple AJAX call, and the server will return either a JSON string with useful data or an error message string produced by the PHP function `mysql_error()`. How can I test whether this data is a JSON string or the error message. It would be nice to use a function called `isJSON` just like you can use the function `instanceof` to test if something is an Array. This is what I want: if (isJSON(data)){ //do some data stuff }else{ //report the error alert(data); }

AI-Generated Solution

Powered by LMSouq AI · GPT-4.1-mini

✓ Solution Ready
Analyzing problem and generating solution…
Was this solution helpful?
Back to Knowledge Base