Storing a client-side array into a json field in the database

I’ve searched all over the community but can’t quite get the answer I need.

I have an array in app connect which stores IDs. I am trying to post that to a server connect API and save it into a JSON field in the database.

I’m posting an input field with the items from the array which is being received by the API as 1,2,3 and when I insert that into the table, the JSON field is saved as "1,2,3"

There’s obviously a key bit I’m not doing. I was expecting the inserted JSON to be ["1","2","3"]

Can anyone steer me in the right direction?

The $_POST variable is set as Type: Array and I’ve tried with multiple ticked and unticked.

In the past I’ve sent multiple data using input fields like id[] and then it posts them as id[1], id[2], etc. and then I loop through them in the API but I’m now using an array field in app connect which I’m sure must be able to be handled in a much simpler way?

Is it because I’m posting a single variable with the value set as the array items? How else should I do it?

Try using .split(’,’) on the post variable. That will turn your string into an array.

1 Like

Thanks Ken. Works a treat. I thought that was probably the solution. I also almost had money on you being the one to provide the answer. :clap:

However, what if the data includes commas? In my case it doesn’t, but surely setting a variable as an array and using the array in app connect should mean it can handle any values and not need the split process?

1 Like

You can stringify your array into an input, and then parse that array on the server side. There’s some posts here in the forum about this, but basically, you stringify the array on the client side, and then use parseJSON() on the POST var of the input received at the server. From there, you just treat it like any other array.

1 Like

please, can you give an example on the Wappler UI ?

No problem. Here are some screenshots to illustrate the process.

1 Like

thank you very much

EDIT json field can be used in MySQL too or only in posgreSQL?

MySQL is fine. That’s what I’m using.

1 Like