JSON vs. Serialized Array in database

PhpMysqlSerializationMysql Json

Php Problem Overview


What are the advantages and disadvantages of storing JSON data in MySQL database vs. serialized array?

Php Solutions


Solution 1 - Php

  1. JSON [encode]1 & [decode]2
  • PHP Version >= 5.0.0
    • Nesting Limit of 20.
  • PHP Version >= 5.2.3
    • Nesting Limit of 128.
  • PHP Version >= 5.3.0
    • Nesting Limit of 512.
  • Small footprint vs PHP's serialize'd string.
  1. [serialize]3 & [unserialize]4
  • PHP Version >= 4.0.0
    • Methods are not lost on PHP Datatype Object.
    • __wakeup() magic method called on any object being unserialize. (VERY POWERFUL)
    • It has been noted that it is some times best the [base64 encode][5] strings put into the database, and [base64 decode][6] strings taken out of the database with this function, as there are some issues with the handling of some white space characters.

The choice is yours.

[1]: http://www.php.net/manual/en/function.json-encode.php "json_encode" [2]: http://www.php.net/manual/en/function.json-decode.php "json_decode" [3]: http://www.php.net/manual/en/function.serialize.php "serialize" [4]: http://www.php.net/manual/en/function.unserialize.php "unserialize" [5]: http://php.net/manual/en/function.base64-encode.php "base64_encode" [6]: http://php.net/manual/en/function.base64-decode.php "base64_decode"

Solution 2 - Php

Pro JSON:

  • The JSON data can be used by many different languages, not just PHP
  • JSON data is human readable and writable.
  • It takes up less space
  • It is faster to encode JSON than to serialize

Pro Serialized Array:

  • It is faster do unserialize than to JSON decode

As the comments indicate, JSON takes up less space than a serialize array. I also checked whether JSON or Serializing is faster, and surprisingly, it is faster to JSON encode than to Serialize. It is faster to unserialize than to JSON decode though.

This is the script I used to test:

<?php 
function runTime(){
      $mtime = microtime(); 
      $mtime = explode(' ', $mtime); 
      $mtime = $mtime[1] + $mtime[0]; 
      return $mtime; 
}
?> 
<pre>
<?php
$start = runTime();

$ser;

for($i=0; $i<1000; $i++){
	$a = array(a => 1, x => 10);
	$ser = serialize($a);
}
$total = runTime() - $start;
echo "Serializing 1000 times took \t$total seconds";
?>

<?php
$start = runTime();

$json;

for($i=0; $i<1000; $i++){
	$a = array(a => 1, x => 10);
	$json = json_encode($a);
}
$total = runTime() - $start;
echo "JSON encoding 1000 times took \t$total seconds";
?>

<?php
$start = runTime();

$ser;

for($i=0; $i<1000; $i++){
	$a = unserialize($ser);
}
$total = runTime() - $start;
echo "Unserializing 1000 times took \t$total seconds";
?>

<?php
$start = runTime();

$json;

for($i=0; $i<1000; $i++){
	$a = json_decode($json);
}
$total = runTime() - $start;
echo "JSON decoding 1000 times took \t$total seconds";
?>
</pre>

Solution 3 - Php

Portability: Winner JSON. JSON is supported on a wider variety of platforms, while PHP de-serialization is only supported (as far as I know) by PHP. While it's possible to parse either format in any language, JSON has more pre-built libraries.

Future Proof: Winner JSON. JSON is a "standard", in the sense that Javascript is a standard, and isn't likely to change anytime in the future. The PHP group has made no promises about the future of the serialization format, and while it's unlikely to change in the future, the fact that a single group controls the format means you may end up with future data that's unreadable.

Fidelity: Winner PHP. PHP serialization will allow you to store data with native PHP data types, including objects defined by custom classes. JSON will only allow you to store generic primitive types, lists of primitive types ("arrays") and key/value pair Objects. PHP Serialization may provide some advantages here if you're developing a PHP application.

File Size: JSON has a slight win here, as PHP's current serialization format is more verbose (as it's storing more information).

Performance: Who knows, it depends, profile.

Conclusion: Go with JSON unless you have a compelling reason to use PHP Serialization .

Solution 4 - Php

JSON is more portable, i.e. you can more easily read/write to it from different languages etc. If you used PHP serialized arrays you would only be able to easily use PHP to access it.

Solution 5 - Php

Are you using your datas only with PHP ? If yes : arrays, if no : JSON.

Pro Array

  • sessions used serialization : I think it's faster than json_encode/decode (not quite sure)
  • many functions on arrays in PHP (sorting/merging/...)

Pro JSON

  • JSON is know in other languages and web languages
  • less verbose in database
  • many tools, like XML : JSON SChema

Solution 6 - Php

There was a lot of such questions on SO.

https://stackoverflow.com/questions/804045/preferred-method-to-store-php-arrays-jsonencode-vs-serialize

In short: JSON is better for simple data, but it doesn't distinguish difference between object and associative array. Serialized data are bigger.

Solution 7 - Php

Use json for for arrays and communication with Javascript or other language. Use serialize for object or any internal PHP work for the current running script.

Solution 8 - Php

if youre trying to get around quotes and special characters in your JSON.stringify(obj) ,you can do so in PHP using it's database-specific escaping methods.

<?php
mysql_real_escape_string(htmlspecialchars($value))
?>

you can now store this safely and decode it when you read it back out

Solution 9 - Php

I just had this big problem with php serialize. I stored a lot of data in a single field in which i used unserialize to read.

What happened is that I got some corrupt data in that field. Serialize map the data with codes like 'a','s' and 'N'. If there is corrupt data, the map failed. It will show a php error that the unserialize function is unable to work because of byte code error.

So my point is to avoid serialize. Go with JSON, way safer and you won't bang your head on future majors problems.

For me, no more serialize.

Solution 10 - Php

JSON beats serialization as most answers already pointed out. I think the biggest advantage is its platform independency. You might have other applications communicate with you database and they might not have anything to do with php.

But both solutions violate database normalization. You database will not even be in first normal form so you cannot take advantage of any database feature like, say, searching. A better approach is to use object relational mapping. There are good libraries out there - consider for example doctrine ORM.

Solution 11 - Php

atm, json_encode() works only with UTF-8 encoded data.. therefore, it cannot encode characters such as "ñ", else it returns NULL

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionInezView Question on Stackoverflow
Solution 1 - PhpMark TomlinView Answer on Stackoverflow
Solution 2 - PhpMariusView Answer on Stackoverflow
Solution 3 - PhpAlan StormView Answer on Stackoverflow
Solution 4 - PhpTom HaighView Answer on Stackoverflow
Solution 5 - Phpmere-teresaView Answer on Stackoverflow
Solution 6 - PhpThinkerView Answer on Stackoverflow
Solution 7 - PhplaurensView Answer on Stackoverflow
Solution 8 - PhptimView Answer on Stackoverflow
Solution 9 - PhpSequenceDigitale.comView Answer on Stackoverflow
Solution 10 - PhpFrancois BourgeoisView Answer on Stackoverflow
Solution 11 - PhpRonaldView Answer on Stackoverflow