Skip to main content

Hi ,

 

I have a requirement where I have to convert JSON file input.txt to csv file.

I am new to dataweave hence finding difficulty to deal with object and array to string conversion.

 

Also wherever there is multiple fields /objects/array/list are present these has to concate with '|' and map to one of the column.

 

Thanks

 

6 Antworten
  1. 10. Nov. 2017, 19:21

    @ajaytiwa12 , please use the Dataweave transformation below as a reference for you sample input. Adjust as needed.

     

    %dw 1.0

    %output application/json

    %function smartcat (s,t) (s ++ "__" ++ t) when not (s is :empty) otherwise t

    %function flatObject(keypath,element)

    element match {

    e is :object -> e mapObject flatObject(smartcat(keypath,$$),$),

    e is :array -> (e map flatObject(keypath,$) reduce (reduceArray($$,$))) when not e is :empty otherwise {},

    e is :null -> {},

    default -> {

    (keypath) : element

    }

    }

    %function reduceArray (ac,val)

    ac mapObject {

    ($$) : $ ++ "|" ++ val[$$]

    }

    ---

    flatObject("",payload)

    Basically, the root object and any child object is recursively flatten. For array types, every element of the array is flatten too and then reduced to a single object using custom function based on mapObject. Empty arrays is silently ignored.

     

    This is not a trivial use case in DW language and I would recommend that you read DW language documentation and also reading about mach type, map, mapObject, concat, reduce operators.

     

    Finally, the DW above creates a single JSON object. It is very easy to change output to CSV with the output declaration but the payload needs to be wrapped into an array as CSV writer would expect a list of records and your input is a single object.

     

    Something like this:

     

    %dw 1.0

    %output application/csv

    %function smartcat (s,t) (s ++ "__" ++ t) when not (s is :empty) otherwise t

    %function flatObject(keypath,element)

    element match {

    e is :object -> e mapObject flatObject(smartcat(keypath,$$),$),

    e is :array -> (e map flatObject(keypath,$) reduce (reduceArray($$,$))) when not e is :empty otherwise {},

    e is :null -> {},

    default -> {

    (keypath) : element

    }

    }

    %function reduceArray (ac,val)

    ac mapObject {

    ($$) : $ ++ "|" ++ val[$$]

    }

    ---

    [flatObject("",payload)]

0/9000