简体   繁体   中英

Retrive value from payload and convert to CSV with Dataweave

Hi I have this example payload. And I would save it to CSV but I don't need all value only a few.

{
    "ex": "names",
    "At": 0,
    "start": 50,
    "total": 506,
    "TypeI": [
        {
            "firstOne": "operations",
            "id": "28",
            "key": "192",
                "timespent": null,
                "project": {
                    "id": "10",
                    "key": "2",
                    "name": "PSM2",
                    "projectTypeKey": "software"
                }
            }
        },
        {
            "firstOne": "opera",
            "id": "27778",
            "key": null,
                "timespent": null,
                "project": {
                    "id": "10",
                    "key": "2",
                    "name": "PSM2",
                    "projectTypeKey": "software"
                }
        
            }
         }   
    ]
}

I don't want change the structure of this payload. I need only a few value from this and then I would like write it to CSV.

I got this payload with the same key and now I would like to save it to CSV. I have problem with the type. Now I got the string. But I need the array of object to save it in CSV.

{
  "TypeI": [
    {
      "firstOne": "operations",
       "date": "2022-03-25T14:12:25.702+0100",
      "id": "28",
      "key": "192"
    },
    {
      "firstOne": "opera",
      "date": null default " ", 
      "id": "27778",
      "key": null default " "
    }
  ]
}

This is example how it would like the CSV

firstOne;date;id;key
operations;2022-03-25T14:12:25.702+0100;28;192
opera;  ;27778;

Ok so I solve one problem.

    %dw 2.0
    import update from dw::util::Values
    output application/json
    ---
     TypeI : (payload.TypeI map ((item, index) -> {
        "firstOne": item.firstOne,
        "date": item.court.date,
        "id": item.id,
        "key": item.key
    } ))

And I got the json without change the structure of my json. Now the second part my question to save this to CSV.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM