skip to Main Content

I’m part of a team, using json_encode() to change an array into a JSON string. I then save this string in my database. Our ORM then escapes the quotes in the string, so if I look in the database, the string is stored as something like this:

"{"id":6,"name":"Number five","color":"red"}"

I can obviously write a method to remove the escaping, but I wanted to check first: Is there an existing, built-in function for unescaping?

====

EDIT: The value is stored this way in the database. Even looking via the command-line mysql command shows this. And when it is re-fetched in my application, it also shows up with the slashes.

2

Answers


  1. i hope this can help you:

    //let's create a table test widh  just an int field and  a text field 
    //where we will put our json string
    //==================================================================
    $mysqli = new mysqli("example.com", "user", "password", "database");
    $mysqli->query("DROP TABLE IF EXISTS test");
    $mysqli->query("CREATE TABLE test(id INT, json_string TEXT)");
    //==================================================================
    
    //i create a json string just like the one used in the original request
    //and i put it in a variable called $jsonString.
    //==================================================================
    $obj = ["id"=>6,"name"=>"Number five","color"=>"red"];
    $jsonString = json_encode($obj);
    //==================================================================
    
    
    
    //let's reproduce the problem:
    //i insert in $jsonString in db as it is.
    //==================================================================
    $stmt = $mysqli->prepare("INSERT INTO test(id, json_string) VALUES (?, ?)");
    $id = 1; //i've created  a table without primary and autoincrement so i need to set an id.
    $stmt->bind_param("ds", $id, $jsonString);
    $stmt->execute();
    ////==================================================================
    
    
    //now let's solve the problem:
    //i insert in $jsonString in db 
    //using the base64_encode($jsonString) and from_base64(?) workaround.
    //==================================================================
    $b64String=base64_encode($jsonString);
    $stmt = $mysqli->prepare("INSERT INTO test(id, json_string) VALUES (?, from_base64(?))");
    $id = 2; //i've created  a table without primary and autoincrement so i need to set an id.
    $stmt->bind_param("ds", $id, $b64String);
    $stmt->execute();
    ////==================================================================
    
    
    //check the result:
    //====================================================================
    $result = $mysqli->query('SELECT * FROM test');
    var_dump($result->fetch_all(MYSQLI_ASSOC));
    //====================================================================
    
    Login or Signup to reply.
  2. you can do it by using stripslashes function of php.The stripslashes() function removes backslashes.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search