Bug #78765 [Com]: enhancement to json_decode : treat a JSON string with duplicate keys as invalid
| From: | danack@php.net | Date: | Thu, 31 Oct 2019 10:43:00 +0000 |
| Subject: | Bug #78765 [Com]: enhancement to json_decode : treat a JSON string with duplicate keys as invalid | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-223534@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=78765&edit=1
ID: 78765
Comment by: danack@php.net
Reported by: bas5winkel at gmail dot com
Summary: enhancement to json_decode : treat a JSON string
with duplicate keys as invalid
Status: Open
Type: Bug
Package: JSON related
Operating System: Debian
PHP Version: 7.2.24
Block user comment: N
Private report: N
New Comment:
To note one thing, although having some way of indicating duplicate keys sounds like a useful thing
(as opposed to silently discarding them), the JSON format does not appear to forbid duplicate keys:
From http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf
"The JSON syntax does not impose any restrictions on
the strings used as names, does not require that name strings be unique, and does not assign any
significance to the ordering of name/value pairs. These are all semantic considerations that may be
defined by
JSON processors or in specifications defining specific uses of JSON for data interchange."
Previous Comments:
------------------------------------------------------------------------
[2019-10-31 10:31:23] bas5winkel at gmail dot com
Description:
------------
Duplicate key names are not allowed in the JSON format.
If a json string containing duplicate keys is decoded with json_decode,
json_decode simple overwrites the existing key.
Just like json_encode returns NULL for other invalid JSON strings,
could such an input also be treated as invalid?
Maybe as an optional check as this is just a rare condition?
Test script:
---------------
<?php
echo
var_export(json_decode('{"a":"b","a":"c"}'),1);
?>
Expected result:
----------------
NULL or an exception
Actual result:
--------------
stdClass::__set_state(array(
'a' => 'c',
))
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=78765&edit=1