DB payload to JSON transformation in mulesoft

93 Views Asked by At

[enter image description here][1]my expected output is below,

{
  "TicketNo":6255,
  "Name": "Issac",
  "Age": 20,
  "Code": "IND354"
}

but I am getting below while transforming the db payload to Json

{
  "TicketNo": [
    6255
  ],
  "Name": [
    "Issac"
  ],
  "Age": [
    20
  ],
  "Code": [
    "IND354"
  ]
}

attaching my RAML, datatype and values body:

          application/json:
            type: Flight
            examples:
              !include outfile.raml 

#%RAML 1.0 DataType

type: object
properties: 
  TicketNo: integer
  Name: string
  Age: integer
  Code: string

--outfile.raml---
#%RAML 1.0 NamedExample
value:
    TicketNo: 1 
    Name: Issac
    Age: 29
    Code: IND354

payload from database [1]: https://i.stack.imgur.com/hm01n.png

1

There are 1 best solutions below

2
aled On

The database query is returning an array of objects. In your example it is returning an array of 1 element. It seems that you are trying to transform the payload directly without considering it is an array, so your transformation returns arrays for each value.

One way to implement the transformation is to select only the first element of the array. This assumes that you only want one object in the response and the query returns at least one object in the response array.

%dw 2.0
output application/java
var firstElement=payload[0]
---
{
    TicketNo: firstElement.TicketNo,
    Name: firstElement.Name,
    Age: firstElement.Age,    
    Code: firstElement.Code
}