1

I am new to Mule 4, Is there a better way to transform this below input payload. The input payload is a list but contains just one item. How can I remove indexing

{
  "id": "123",
  "result": "SUCCESS",
  "code": "200",
  "application": "api",
  "provider": "sql",
  "payload": {
    "employeeInfo": [
      {
        "first_name": "kate",
        "last_name": "turner",
        "email": "kate.turner@gmail.com ",
        "phone_number": 1234567890,
        "hire_date": "2023-07-03",
        "job_id": 145,
        "employee_id": 987654,
        "manager_id": 365,
        "department_id": 3
      }
    ]
  }
}

Dataweave

%dw 2.0
output application/json
---
"payload": {
"employeeId": payload.payload.employeeInfo[0].employee_id,
"firstName": payload.payload.employeeInfo[0].first_name,
"lastName": payload.payload.employeeInfo[0].last_name,
"EmailId": payload.payload.employeeInfo[0].email,
"PhoneNumber": payload.payload.employeeInfo[0].phone_number,
"HireDate": payload.payload.employeeInfo[0].hire_date
}
Sunny85
  • 37
  • 1
  • 6
  • 1
    You should provide expected output and actual output for this type of question. Also "removing indexing" is unclear. I'm guessing that you want to remove the array, but readers of your question should not need to guess what your question means. Additionally if you are asking for "a better way" you should explain what is your criteria for "better". – aled Jul 08 '23 at 23:48
  • It is still an array even if there is one element. If you are concerned about repeated expressions with index you can store `payload.payload.employeeInfo[0]` in a variable and reuse it in your expression – Harshank Bansal Jul 09 '23 at 11:44
  • Noted Aled, will update expected output in any of my next questions. Thanks, Thanks Harshank for your inputs. – Sunny85 Jul 16 '23 at 23:32

1 Answers1

1

If I understand correctly what you are asking is to replace the array at payload.payload.employeeInfo by its first element. Using the update operator is just a matter of just pointing to that element and use the first element, at index 0, to replace its value.

%dw 2.0
output application/json
---
payload  update {
    case a at .payload.employeeInfo -> a[0]
}

Output:

{
  "id": "123",
  "result": "SUCCESS",
  "code": "200",
  "application": "api",
  "provider": "sql",
  "payload": {
    "employeeInfo": {
      "first_name": "kate",
      "last_name": "turner",
      "email": "kate.turner@gmail.com ",
      "phone_number": 1234567890,
      "hire_date": "2023-07-03",
      "job_id": 145,
      "employee_id": 987654,
      "manager_id": 365,
      "department_id": 3
    }
  }
}

Input:

{
  "id": "123",
  "result": "SUCCESS",
  "code": "200",
  "application": "api",
  "provider": "sql",
  "payload": {
    "employeeInfo": [
      {
        "first_name": "kate",
        "last_name": "turner",
        "email": "kate.turner@gmail.com ",
        "phone_number": 1234567890,
        "hire_date": "2023-07-03",
        "job_id": 145,
        "employee_id": 987654,
        "manager_id": 365,
        "department_id": 3
      }
    ]
  }
}
aled
  • 21,330
  • 3
  • 27
  • 34