Zak01
Zak01

Reputation: 17

How to insert an Array into a single column in SQL Table using Mule4 DB insert operator

I am trying to insert an array into a single column in the SQL Table. The data type of the column is varchar but I can change it to anything else if required. I am using DB single insert operator and while inserting the records, I get an error in Mule saying "The conversion from UNKNOWN to VARCHAR is unsupported.". Looks like the SQL insert operator in Mule is not recognizing the array type and does not know what to do with it. Below is the sampel data that I need to insert. The whole array after payloqd needs to be in a single column in SQL Table

 { "payload":[
    {
        "Name":"JOhn",
        "StartDatetime": "2022-11-23 4:01:59.329",
        "EndDatetime":  "2022-11-23 4:01:59.329"

    },
    {
        "Name":"Jay",
        "StartDatetime": "2022-11-23 4:01:59.329",
        "EndDatetime":  "2022-11-23 4:01:59.329"
    },
    {
        "Name":"Boo",
        "StartDatetime": "2022-11-23 4:01:59.329",
        "EndDatetime":  "2022-11-23 4:01:59.329"
    }

]}

I tried to do a simple insert in the SQL Studio Management and I was able to insert the whole array in the column but it does not seem to work in Mulesoft.

Upvotes: 0

Views: 612

Answers (1)

aled
aled

Reputation: 25837

As @HarshankBansal mentioned, you can not store an array in a string. You can however transform the array to a string. For example to store a JSON array as a string. You can use the write() function to 'print' a value to a string. The output of write() is a string in the format specified in the second parameter.

Example:

write(payload.payload, "application/json")

Upvotes: 1

Related Questions