Search code examples
jsonduplicateskeyjqon-duplicate-key

JSON fields have the same name


In practice, keys have to be unique within a JSON object (e.g. Does JSON syntax allow duplicate keys in an object?). However, suppose I have a file with the following contents:

{
    "a" : "1",
    "b" : "2",
    "a" : "3"
}

Is there a simple way of converting the repeated keys to an array? So that the file becomes:

{
    "a" : [ {"key": "1"}, {"key": "3"}],
    "b" : "2"
}

Or something similar, but which combines the repeated keys into an array (or finds and alternative way to extract the repeated key values).

Here's a solution in Java: Convert JSON object with duplicate keys to JSON array

Is there any way to do it with awk/bash/python?


Solution

  • If your input is really a flat JSON object with primitives as values, this should work:

    jq -s --stream 'group_by(.[0]) | map({"key": .[0][0][0], "value": map(.[1])}) | from_entries'
    
    {
      "a": [
        "1",
        "3"
      ],
      "b": [
        "2"
      ]
    }
    

    For more complex outputs, that would require actually understanding how --stream is supposed to be used, which is beyond me.