saravanan suriya
saravanan suriya

Reputation: 21

Jolt specification needed for the below input and expected output

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"
    }
  ]
}

Desired 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[]"
            }
          }
        }
      }
    }
  }
]

Upvotes: 2

Views: 213

Answers (1)

Jagadesh
Jagadesh

Reputation: 2116

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[]"
            }
          }
        }
      }
    }
  }
]

Upvotes: 1

Related Questions