Welcome to OGeek Q&A 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...