[php-src] PR #22473: Add json_decode flags for duplicate JSON object keys
| From: | PurHur | Date: | Fri, 26 Jun 2026 16:14:18 +0000 |
| Subject: | [php-src] PR #22473: Add json_decode flags for duplicate JSON object keys | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-36677@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/22473
Author: PurHur
## Summary
JSON objects can legally repeat the same key (RFC 8259 does not forbid it), but PHP has only ever
given you one outcome: the last value wins. That is a fine default, yet it is not great when you are
parsing data that actually relies on duplicate keys and you need either every value or a controlled
merge — not an implicit overwrite or a full deep merge every time.
This PR adds two opt-in
json_decode() flags:
- JSON_DUPLICATE_KEY_ARRAY — collect duplicate values for the same key into a list
- JSON_DUPLICATE_KEY_MERGE — recursively merge nested objects/arrays when a key
repeats; scalars still overwrite (same as today)
If you do not pass either flag, nothing changes. The two flags cannot be combined.
The new behavior is wired through the existing JSON parser method table, so the default decode path
keeps using the original object_update implementation.
## Motivation
Mainly std compatibility / predictability around duplicate keys. Right now callers have to
pre-process JSON or work around last-key-wins if their format uses repeated names on purpose. Having
explicit flags makes that choice visible instead of hoping the parser does what you need.
## Test plan
- [x] make test TESTS=ext/json/tests/ — all JSON tests pass
- [x] PHPT coverage for default last-key-wins BC, merge, array, and invalid flag combination
- [x] Verified default-path output matches master for a range of decode scenarios
Happy to move this through an RFC / internals discussion if that is required before merge.
Made with [Cursor](https://cursor.com)