kambythet
kambythet

Reputation: 716

how to use mulesoft dataweave to transform to json with grouping and string to array

I have a database call the provides a payload as described below. how I do use dataweave to transform that payload to json in the format as provided below the example table?

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|    company    |status|    license_id    |acct status|   last_inv_date   |  acctnum |      owner      | entlmt |            roles          |subscribed|attr_type|        attr_key       |attr_value|
|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| company name 1|Active|02iq0000000xlBBAAY|   Active  |2016-02-25 22:50:04|A100001135|[email protected]|Standard|Admin;wcl_admin;wcl_support|     1    |  cloud  |cloud_num_247_t_streams|     1    |
|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| company name 1|Active|02iq0000000xlBBAAY|   Active  |2016-02-25 22:50:04|A100001135|[email protected]|Standard|Admin;wcl_admin;wcl_support|     1    |  cloud  |      api_access       |     1    |
|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| company name 1|Active|02iq0000000xlBBAAY|   Active  |2016-02-25 22:50:04|A100001135|[email protected]|Standard|Admin;wcl_admin;wcl_support|     1    |  cloud  |cloud_num_247_p_streams|     1    |
|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| company name 2|Active|02iq0000000xlBBBBZ|   Active  |2016-02-25 22:50:04|A100001166|[email protected]|Standard|           Admin           |     1    |  cloud  |cloud_num_247_p_streams|     0    |
|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| company name 2|Active|02iq0000000xlBBBBZ|   Active  |2016-02-25 22:50:04|A100001166|[email protected]|Standard|           Admin           |     1    |  cloud  |       api_access      |     1    |
|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

Final output desired in json:

{
  "records": [
    {
      "company": "company name 1",
      "has_active_subscriptions": true,
      "license_status": "Active",
      "license_id": "02iq0000000xlBBAAY",
      "account_status": "Prospect",
      "last_invoice_date": "2016-02-25 22:50:04",
      "cloud_owner_email": "[email protected]",
      "role": [
        "Admin",
        "wcl_admin",
        "wcl_support"
      ],
      "account_number": "A100001135",
      "attributes": {
        "cloud": {
          "api_access": 1,
          "cloud_num_247_t_streams": 1,
          "cloud_num_247_p_streams": 1
        }
      },
      "entitlement_plan": "Standard"
    },
    {
      "company": "company name 2",
      "has_active_subscriptions": true,
      "license_status": "Active",
      "license_id": "02iq0000000xlBBBBZ",
      "account_status": "Active",
      "last_invoice_date": "2016-02-25 22:50:04",
      "cloud_owner_email": "[email protected]",
      "role": [
        "Admin"
      ],
      "account_number": "A100001166",
      "attributes": {
        "cloud": {
          "cloud_num_247_p_streams": 0,
          "api_access": 1
        }
      },
      "entitlement_plan": "Standard"
    }
  ]
}

Upvotes: 0

Views: 1544

Answers (2)

Manik Magar
Manik Magar

Reputation: 1401

You dont need DataWeave if you just want to transform resultset into JSON. You can use ObjectToJson transformer to do that.

Upvotes: 0

Supposing that the dataweave component is just after the database component, and the result of the query is still on the payload: the payload is then an ArrayList of CaseInsensitiveHashMap - similar to the records object on your JSON.

So I would try something like:

%dw 1.0
%output application/json

records: payload

Upvotes: 1

Related Questions