Issue Details
- Number
- 20949
- Title
- Handling of duplicate keys in JSON
- Description
- Our handling of duplicate keys in JSON dictionaries is different from that of other languages like Python, Javascript (and most others I know of).
While those languages parse the JSON and build hash tables, the last assignment of a key is what takes effect. e.g.
```python
>>> json.loads('{"a": "first", "a": "second"}')
{'a': 'second'}
```
Our `univalue`, though, stores every (key,value) pair in an array, and when a key is queried it returns the *first* associated value.
```c++
#include <iostream>
#include <univalue.h>
int main() {
UniValue val;
val.read("{\"a\": \"first\", \"a\": \"second\"}");
std::cout << val["a"].write() << std::endl;
return 0;
}
```
```bash
$ g++ unitest.cpp -o unitest -Iunivalue/include -L src/univalue/.libs -lunivalue
$ ./unitest
"first"
```
This is not generally a problem but can create subtle interoperability issues. For example a proxy that validates the allowed JSON-RPC commands and lets through only some combinations of command/argument (and doesn't take this into account) could be misled in this way.
Either reversing this order or adding a "strict mode" that rejects duplicate keys might make sense. I don't know.
(issue originally reported by Florian Mathieu)
- URL
-
https://github.com/bitcoin/bitcoin/issue/20949
- Closed by
-
Back to List