Req #78765 [Com]: enhancement to json_decode : treat a JSON string with duplicate keys as invalid

From: Date: Thu, 07 Oct 2021 12:34:44 +0000
Subject: Req #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-237082@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: arunsharmaji982 at gmail dot com Reported by: bas5winkel at gmail dot com Summary: enhancement to json_decode : treat a JSON string with duplicate keys as invalid Status: Open Type: Feature/Change Request Package: JSON related Operating System: Debian PHP Version: 7.2.24 Block user comment: N Private report: N New Comment: https://www.onlinevashikaranspecialists.com/love-problem-solution-near-me/ https://www.onlinevashikaranspecialists.com/love-problem-solution-in-delhi/ https://www.onlinevashikaranspecialists.com/love-marriage-specialist-near-me/ https://www.onlinevashikaranspecialists.com/extra-marital-affairs-problem-solution/ https://www.onlinevashikaranspecialists.com/black-magic-specialist-near-me/ Previous Comments: ------------------------------------------------------------------------ [2019-11-10 04:22:04] php-bugs at lists dot php dot net No feedback was provided. The bug is being suspended because we assume that you are no longer experiencing the problem. If this is not the case and you are able to provide the information that was requested earlier, please do so and change the status of the bug back to "Re-Opened". Thank you. ------------------------------------------------------------------------ [2019-10-31 10:53:54] cmb@php.net In my opinion, that would be a sensible improvement. From a quick look, it might be sufficient to check whether respective entries already exist in php_json_parser_object_update[1], and to set an error flag in this case. And/or add an option, and return NULL or throw an exception if there are duplicate entries. A pull request would be welcome! [1] <https://github.com/php/php-src/blob/master/ext/json/json_parser.y#L250-L269> ------------------------------------------------------------------------ [2019-10-31 10:44:07] requinix@php.net Same for IETF: > An object whose names are all unique is interoperable in the sense that all software > implementations receiving > that object will agree on the name-value mappings. When the names within an object are not > unique, the behavior > of software that receives such an object is unpredictable. Many implementations report the > last name/value pair > only. Other implementations report an error or fail to parse the object, and some > implementations report all of > the name/value pairs, including duplicates. https://tools.ietf.org/html/rfc8259#section-4 ------------------------------------------------------------------------ [2019-10-31 10:43:00] danack@php.net 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." ------------------------------------------------------------------------ [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

« previous php.bugs (#237082) next »