I need to check if an option value from database is a json object.

This funtion following cannot work :

function isJson( string $str, $is_assoc = true ){
    json_decode( $str, $is_assoc );
    return (json_last_error() == JSON_ERROR_NONE);

It´s normal because with an SQL query I got :


instead of

{ '3' : { 'title' : "", 'text' : "..." }...}}

How can I do to check if this data is a json object and to get this data as object ?

Final goal : Reaplace correclty value for json object in the database without breaking serialized data.

  • Do you have control of the data? is it prepared by yourself before saving it to database? Did you try unserialize it for further manipulations? – simongcc Apr 7 '20 at 17:32

To check if a string from database is like a:2:{i:3;a:4:{s:5:"title";s:0:"";s:4:"text";s:503:"...}}, you can use the function is_serialized()

To get the data as php array :

    $data_as_array = unserialize( $data_as_string);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.