php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #38945 json_decode has limits on array depth.
Submitted: 2006-09-25 07:42 UTC Modified: 2007-05-25 19:29 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: RQuadling at GMail dot com Assigned: bjori (profile)
Status: Closed Package: Documentation problem
PHP Version: Irrelevant OS: Windows XP SP2
Private report: No CVE-ID: None
 [2006-09-25 07:42 UTC] RQuadling at GMail dot com
Description:
------------
Massively nested arrays are decoded as null by json_decode.

Admittedly, an array this deep has its own concerns, but with no warnings or notices, documentation of the limit would be advisable.

The following code has 2 arrays, the first is 18 levels, the second is 19. The first decodes correctly, the second decodes as null.

Whilst an array MAY not be this deep, it is possible that objects containing objects with arrays COULD produce a depth of 19 levels. Sure, not exactly LEAN, but that's why this is just a minor doc issue and not a more important code issue.

The issue is with json_decode as json_encode quite happily encodes the larger array - an inconsistency maybe as the json_decode dox does say any json_encode'd output (though I am awaiting the changes due to bug#38680).

It also makes no difference if you use the 'assoc' option (json_decode's second paramater of True) to produce an associative array. The limit is still reached at 19 levels.

Reproduce code:
---------------
<?php
$a_array = 
	array('One' => array('Two' => array('Three' => array('Four' => 
	array('Five' => array('Six' => array('Seven' => array('Eight' => 
	array('Nine' => array('Ten' => array('Eleven' => array('Twelve' => 
	array('Thirteen' => array('Fourteen' => array('Fifteen' => 
	array('Sixteen' => array('Seventeen' => array('Eighteen' => 
	array(1,2,3)))))))))))))))))));
var_dump(json_encode($a_array));
var_dump(json_decode(json_encode($a_array)));

$a_array = 
	array('One' => array('Two' => array('Three' => array('Four' => 
	array('Five' => array('Six' => array('Seven' => array('Eight' => 
	array('Nine' => array('Ten' => array('Eleven' => array('Twelve' => 
	array('Thirteen' => array('Fourteen' => array('Fifteen' => 
	array('Sixteen' => array('Seventeen' => array('Eighteen' => 
	array('Nineteen' => array(1,2,3))))))))))))))))))));
var_dump(json_encode($a_array));
var_dump(json_decode(json_encode($a_array)));
?>

Expected result:
----------------
string(195) "{"One":{"Two":{"Three":{"Four":{"Five":{"Six":{"Seven":{"Eight":{"Nine":{"Ten":{"Eleven":{"Twelve":{"Thirteen":{"Fourteen":{"Fifteen":{"Sixteen":{"Seventeen":{"Eighteen":[1,2,3]}}}}}}}}}}}}}}}}}}"
object(stdClass)#1 (1) {
  ["One"]=>
  object(stdClass)#2 (1) {
    ["Two"]=>
    object(stdClass)#3 (1) {
      ["Three"]=>
      object(stdClass)#4 (1) {
        ["Four"]=>
        object(stdClass)#5 (1) {
          ["Five"]=>
          object(stdClass)#6 (1) {
            ["Six"]=>
            object(stdClass)#7 (1) {
              ["Seven"]=>
              object(stdClass)#8 (1) {
                ["Eight"]=>
                object(stdClass)#9 (1) {
                  ["Nine"]=>
                  object(stdClass)#10 (1) {
                    ["Ten"]=>
                    object(stdClass)#11 (1) {
                      ["Eleven"]=>
                      object(stdClass)#12 (1) {
                        ["Twelve"]=>
                        object(stdClass)#13 (1) {
                          ["Thirteen"]=>
                          object(stdClass)#14 (1) {
                            ["Fourteen"]=>
                            object(stdClass)#15 (1) {
                              ["Fifteen"]=>
                              object(stdClass)#16 (1) {
                                ["Sixteen"]=>
                                object(stdClass)#17 (1) {
                                  ["Seventeen"]=>
                                  object(stdClass)#18 (1) {
                                    ["Eighteen"]=>
                                    array(3) {
                                      [0]=>
                                      int(1)
                                      [1]=>
                                      int(2)
                                      [2]=>
                                      int(3)
                                    }
                                  }
                                }
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
string(208) "{"One":{"Two":{"Three":{"Four":{"Five":{"Six":{"Seven":{"Eight":{"Nine":{"Ten":{"Eleven":{"Twelve":{"Thirteen":{"Fourteen":{"Fifteen":{"Sixteen":{"Seventeen":{"Eighteen":{"Nineteen":[1,2,3]}}}}}}}}}}}}}}}}}}}"
object(stdClass)#1 (1) {
  ["One"]=>
  object(stdClass)#2 (1) {
    ["Two"]=>
    object(stdClass)#3 (1) {
      ["Three"]=>
      object(stdClass)#4 (1) {
        ["Four"]=>
        object(stdClass)#5 (1) {
          ["Five"]=>
          object(stdClass)#6 (1) {
            ["Six"]=>
            object(stdClass)#7 (1) {
              ["Seven"]=>
              object(stdClass)#8 (1) {
                ["Eight"]=>
                object(stdClass)#9 (1) {
                  ["Nine"]=>
                  object(stdClass)#10 (1) {
                    ["Ten"]=>
                    object(stdClass)#11 (1) {
                      ["Eleven"]=>
                      object(stdClass)#12 (1) {
                        ["Twelve"]=>
                        object(stdClass)#13 (1) {
                          ["Thirteen"]=>
                          object(stdClass)#14 (1) {
                            ["Fourteen"]=>
                            object(stdClass)#15 (1) {
                              ["Fifteen"]=>
                              object(stdClass)#16 (1) {
                                ["Sixteen"]=>
                                object(stdClass)#17 (1) {
                                  ["Seventeen"]=>
                                  object(stdClass)#18 (1) {
                                    ["Eighteen"]=>
                                    object(stdClass)#19 (1) {
                                      ["Nineteen"] =>
                                        array(3) {
                                        [0]=>
                                        int(1)
                                        [1]=>
                                        int(2)
                                        [2]=>
                                        int(3)
                                      }
                                    }
                                  }
                                }
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

Actual result:
--------------
string(195) "{"One":{"Two":{"Three":{"Four":{"Five":{"Six":{"Seven":{"Eight":{"Nine":{"Ten":{"Eleven":{"Twelve":{"Thirteen":{"Fourteen":{"Fifteen":{"Sixteen":{"Seventeen":{"Eighteen":[1,2,3]}}}}}}}}}}}}}}}}}}"
object(stdClass)#1 (1) {
  ["One"]=>
  object(stdClass)#2 (1) {
    ["Two"]=>
    object(stdClass)#3 (1) {
      ["Three"]=>
      object(stdClass)#4 (1) {
        ["Four"]=>
        object(stdClass)#5 (1) {
          ["Five"]=>
          object(stdClass)#6 (1) {
            ["Six"]=>
            object(stdClass)#7 (1) {
              ["Seven"]=>
              object(stdClass)#8 (1) {
                ["Eight"]=>
                object(stdClass)#9 (1) {
                  ["Nine"]=>
                  object(stdClass)#10 (1) {
                    ["Ten"]=>
                    object(stdClass)#11 (1) {
                      ["Eleven"]=>
                      object(stdClass)#12 (1) {
                        ["Twelve"]=>
                        object(stdClass)#13 (1) {
                          ["Thirteen"]=>
                          object(stdClass)#14 (1) {
                            ["Fourteen"]=>
                            object(stdClass)#15 (1) {
                              ["Fifteen"]=>
                              object(stdClass)#16 (1) {
                                ["Sixteen"]=>
                                object(stdClass)#17 (1) {
                                  ["Seventeen"]=>
                                  object(stdClass)#18 (1) {
                                    ["Eighteen"]=>
                                    array(3) {
                                      [0]=>
                                      int(1)
                                      [1]=>
                                      int(2)
                                      [2]=>
                                      int(3)
                                    }
                                  }
                                }
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
string(208) "{"One":{"Two":{"Three":{"Four":{"Five":{"Six":{"Seven":{"Eight":{"Nine":{"Ten":{"Eleven":{"Twelve":{"Thirteen":{"Fourteen":{"Fifteen":{"Sixteen":{"Seventeen":{"Eighteen":{"Nineteen":[1,2,3]}}}}}}}}}}}}}}}}}}}"
NULL

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-11-17 05:00 UTC] grebenshikov dot n at gmail dot com
json_decode also has limits on object depth.

class C2 {
	public $text = 'Hello!';
}

class C1 {
	public $next;
	public $objs = array();
	
	public function __construct($next) {
		$this->next = $next;
		for ($i=1; $i<=10; $i++) {
			array_push($this->objs, new C2());
		}
	}
}
$first = new C1(false);
$obj = $first;

for ($i=1; $i<=17; $i++) {
	$obj->next = new C1(false);
	$obj = $obj->next;	
}
print (json_encode($first));
print_r(json_decode(json_encode($first)));
 [2007-05-25 19:29 UTC] bjori@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

The limit was 20, but was bumped to 128 in 5.2.3
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 10:01:31 2024 UTC