0

I have a JSON like this (edited)


  {
    "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 whenever the status message has one of the expected status to empty the users array.

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

How can this be done with Jolt specs?

2 Answers 2

1

This spec should work to achieve your transformation requirement.

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

If status matches PENDING, INACTIVE or BLOCKED, then only the status field is shifted, else the user object is also shifted.

Using default operation, an empty user array will be set if it's missing, which it will be when status is PENDING, INACTIVE or BLOCKED.

Sign up to request clarification or add additional context in comments.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
1

You can use the following transformation :

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "&",
        "users": {
          "@1,status": {
            "PENDING|INACTIVE|BLOCKED": {
              "@": "&3"//returns "users":null if the matches with the literals above 
            },
            "*": {//if not one of the above listed cases 
              "@2": "&3"
            }
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "~users": [] //set [] if it is null due to the ~ character
    }
  }
]

Edit : You just can remove the one level of wrapper in order to handle this new edited case :

[
  {
    "operation": "shift",
    "spec": {
      // "*": {
      "*": "&",
      "users": {
        "@1,status": {
          "PENDING|INACTIVE|BLOCKED": {
            "@": "&3"
          },
          "*": {
            "@2": "&3"
          }
        }
      }
    }
    // }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "~users": []
    }
  }
]

4 Comments

This solution worked! However, rest of the values in json are appearing as keys for when users = []. e.g. top level key like "region": "US" is appearing as "US": null and elements in other array in users i.e. accounts are assigned at top level with index as keys like "0": { account object 1}, "1": { account object 2}
I think I realized why it is this way.. I provided input JSON as array incorrectly. It is not in an array. Removing the initial "*": "&" fixed the issue
Hi @Sunnydays you'd better editing the question with those details provided within the comments.
Updated json in question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.