Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditional removal of array entries #1277

Open
suyashrails opened this issue Jan 21, 2025 · 2 comments
Open

Conditional removal of array entries #1277

suyashrails opened this issue Jan 21, 2025 · 2 comments

Comments

@suyashrails
Copy link

suyashrails commented Jan 21, 2025

I have a json like this

{
    "status": "ACTIVE",
    "users": [{
        "id": "1",
        "name": "Aaron",
        "accounts": [{
            "id": 11,
            "value": "account_11"
        }, {
            "id": "12",
            "value": "account_12"
        }]
    }, {
        "id": "2",
        "name": "Rodney",
        "accounts": [{
            "id": 22,
            "value": "account_22"
        }]
    }, {
        "id": "3",
        "name": "Clayton",
        "accounts": [{
            "id": 33,
            "value": "account_33"
        }]
    }]
}

I want to keep the users array empty when status is "PENDING", "INACTIVE" or "BLOCKED" but not of any other value of status.

e.g.

  1. for above json no change
  2. for following json
{
    "status": "BLOCKED",
    "users": [{
        "id": "1",
        "name": "Aaron",
        "accounts": [{
            "id": 11,
            "value": "account_11"
        }, {
            "id": "12",
            "value": "account_12"
        }]
    }, {
        "id": "2",
        "name": "Rodney",
        "accounts": [{
            "id": 22,
            "value": "account_22"
        }]
    }, {
        "id": "3",
        "name": "Clayton",
        "accounts": [{
            "id": 33,
            "value": "account_33"
        }]
    }]
}

it should result in

{
    "status": "BLOCKED",
    "users": []
}

How can this be done with Jolt specs?

@jfrost1610
Copy link

jfrost1610 commented Jan 22, 2025

I am going with the assumption that the input json is always an array with just 1 element and the output is expected to be an object with that first element.

[
  {
    "operation": "shift",
    "spec": {
      "0": {
        "status": {
          "PENDING|INACTIVE|BLOCKED": {
            "$": "status"
          },
          "*": {
            "$": "status",
            "@(2,users)": "users"
          }
        }
      }
    }
  },
  {
    "operation": "default",
    "spec": {
      "users": []
    }
  }
]

If your array can contain more than one object and you want to map each then use this:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "status": {
          "PENDING|INACTIVE|BLOCKED": {
            "$": "[&3].status"
          },
          "*": {
            "$": "[&3].status",
            "@(2,users)": "[&3].users"
          }
        }
      }
    }
  },
  {
    "operation": "default",
    "spec": {
      "*": {
        "users": []
      }
    }
  }
]

@suyashrails
Copy link
Author

Thanks for the response. I realized that my input json need not be enclosed in an array. Sorry about the confusion. I suppose the transformation wont change significantly due to this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants