0

Python json.tool can be use this way in the shell command line to process JSON conent:

$ echo '{"x": 1}' | python -mjson.tool
{
    "x": 1
}

Howerver, it seems it doesn't detect duplicated keys:

$ echo '{"x": 1, "x": 2}' | python -mjson.tool
{
    "x": 2
}

Can be json.tool be used in a way (e.g. a flag/parameter) so duplicated keys are detected and an error is raised? Looking to documentation I haven't seen anything for that...

EDIT: this question is not a duplicate of the referred ones, as these questions deal with Python programs to detect duplicated keys while what I'm asking here is about using existing json.tool in shell commands line for that. However, the comments to the question has provided the answer (it is "no") so it's fine.

fgalan
  • 11,732
  • 9
  • 46
  • 89
  • python dict has unique keys – Brown Bear Sep 15 '18 at 10:35
  • 1
    If you look at the [source code](https://github.com/kjirou/python-mjson/blob/master/mjson/tool.py), you will see that it creates a python `dict` and dumps it again. That will always just lose the duplicate keys. So the answer is no. – user2390182 Sep 15 '18 at 10:36
  • This is not "JSON content". "An object is an unordered **set** of name/value pairs." – Reut Sharabani Sep 15 '18 at 10:37
  • There are no duplicate keys in JSON objects and Python dicts by definition. Strictly seen your input is no JSON. – Klaus D. Sep 15 '18 at 10:39
  • JSON objects *are* permitted to have duplicated keys, but the standard doesn't dictate how programs should deal with them. The Python approach is valid, but there are other possibilities. And it's an unfortunate fact of life that some places use such JSON objects. – PM 2Ring Sep 15 '18 at 10:43
  • Tbe accepted answer by jfs in the second link does pretty much what you want: rejects JSON objects with duplicate keys. – PM 2Ring Sep 15 '18 at 10:52

0 Answers0