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

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

mule - Unable to filter Array of objects having dynamic keys using dataweave

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...