Merge objects in an based on a common key/value

38 views Asked by At

I have this input JSON:

[
  {
    "id": "12345",
    "color": "red",
    "job": "plumber",
    "version": "5.2.4",
    "description": "Plumber john likes his job",
    "contact": "true"
  },
  {
    "id": "12345",
    "color": "green",
    "job": "electrician",
    "version": "7.3.9",
    "description": "Electrician Bob is unhappy",
    "contact": "true"
  }
]

I'm trying to get the output of:

[
    {
        "id" : "12345",
        "color" : "red",
        "color" : "green",
        "job" : "plumber",
        "job" : "electrician",
        "version" : "5.2.4",
        "version" : "7.3.9",
        "description" : "Plumber john likes his job",
        "description" : "Electrician Bob is unhappy",
        "contact" : "true"
    }
]

I'm trying to make sure that when the merge occurs off the key of id that any key-value pair that is the same isn't duplicated, but if the key-value pair differs then that will be displayed/merged. For instance above the color has a value of red and green of which I want it to be merged in while contact is true for both so no need to duplicate on the merge.

I'm using the following JOLT to group all the values, but the final output is something I'm not able to achieve.

JOLT:

[
  {
    "operation": "shift",
    "spec": {
      "data": {
        "*": {
          "id": {
            "*": {
              "@2": "&[]"
            }
          }
        }
      }
    }
   },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": "&2.&"
        }
      }
    }
    }
]

0

There are 0 answers