Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to filter this array of object based on EmployeeStatus which should be only Resigned and Terminated but not active. Here the keys are different for each object and hence I am unable to apply logic like item.key.EmployeeStaus=="Resigned" or "Terminated" .Any suggestions or hint would be helpful for me.

Input Payload

[
  {
    "626433000000196190": [
      {
        "EmailID": "[email protected]",
        "Employeestatus": "Resigned"
      }
    ]
  },
  {
    "626433000000196184": [
      {
        "EmailID": "[email protected]",
        "Employeestatus": "Active"
      }
    ]
  },
  {
    "626433000000196178": [
      {
        "EmailID": "[email protected]",
        "Employeestatus": "Active"
      }
    ]
  },
  {
    "626433000000196166": [
      {
        "EmailID": "[email protected]",
        "Employeestatus": "Terminated"
      }
    ]
  }
]

Required Output

[
  {
        "EmailID": "[email protected]",
        "Employeestatus": "Resigned"
  },
  {
        "EmailID": "[email protected]",
        "Employeestatus": "Terminated
  }
]
question from:https://stackoverflow.com/questions/65831199/unable-to-filter-array-of-objects-having-dynamic-keys-using-dataweave

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.2k views
Welcome To Ask or Share your Answers For Others

1 Answer

Maybe something like this:

Input

[
  {
    "626433000000196190": [
      {
        "EmailID": "[email protected]",
        "Employeestatus": "Resigned"
      }
    ]
  },
  {
    "626433000000196184": [
      {
        "EmailID": "[email protected]",
        "Employeestatus": "Active"
      }
    ]
  },
  {
    "626433000000196178": [
      {
        "EmailID": "[email protected]",
        "Employeestatus": "Active"
      }
    ]
  },
  {
    "626433000000196166": [
      {
        "EmailID": "[email protected]",
        "Employeestatus": "Terminated"
      }
    ]
  }
]

Script

%dw 2.0
output application/json
---
flatten(payload   map 
     ($  mapObject {
          b: $ filter ($.Employeestatus != "Active") 
     }).b -[])

Output

[
  {
    "EmailID": "[email protected]",
    "Employeestatus": "Resigned"
  },
  {
    "EmailID": "[email protected]",
    "Employeestatus": "Terminated"
  }
]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...