Jolt specification needed for the below input and expected output

Viewed 97

I need to split the data list into two separate idExists and idNotExists lists, based on whether id exists or not.

Can someone help me with Jolt Specification to achieve the below results?

Input:

{
  "data": [
    {
      "name": "a",
      "id": "100"
    },
    {
      "name": "b",
      "id": "101"
    },
    {
      "name": "c"
    }
  ]
}

Output:

{
  "IdExists": [
    {
      "name": "a",
      "id": "100"
    },
    {
      "name": "b",
      "id": "101"
    }
  ],
  "IdNotExists": [
    {
      "name": "c"
    }
  ]
}

I have tried with the below spec but it is not working as expected

[
  {
    "operation": "modify-default-beta",
    "spec": {
      "data": {
        "": {
          "id": "NotExist"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "data": {
        "": {
          "id": {
            "10*": {
              "@2": "IdExists[]"
            },
            "*": {
              "@2": "IdNotExists[]"
            }
          }
        }
      }
    }
  }
]
1 Answers

I had some modification on your spec, added * as it was missing in the selectors.

Modified shifting condition to NotExist and finally shift only name to the IdNotExists array.

[
  {
    "operation": "modify-default-beta",
    "spec": {
      "data": {
        "*": {
          "id": "NotExist"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "data": {
        "*": {
          "id": {
            "NotExist": {
              "@(2,name)": "IdNotExists[].name"
            },
            "*": {
              "@2": "IdExists[]"
            }
          }
        }
      }
    }
  }
]
Related