How would do an INSERT with JSON data from an API all in a server action?

Hi All,

I have some data being returned in JSON from an API request.

I need to write this into my database before it can be displayed and used in the front-end.

High-level, how do I go about this in the server action?

I can’t seem to get it working as I’m naturally use to using form inputs all the time for inserting data

Do you want to insert the whole JSON response into a field, or convert the JSON into useable data that you can insert various data points into various fields?

The second one :slight_smile:

For the most part it would probably just be a handful of fields/columns. But many rows of data.

Cool.

First you’ll need to unravel the data from the JSON on the Server Action - there is a reported bug currently that nested JSON is not showing in it’s entirety. My workaround is to manually build the schema (or add to it) in the API output, or use a Set Value. Others will recommend a repeat for this.

I’d say if you have few fields, but large rows of data then repeat on the data then a multi-insert is probably how you would do it.

My schema is showing up ok (I saw your bug post) as its not that nested.

So you’re suggesting using the set-value action? I mean it seems its the element in the server action that can grab the value… so it must be it

I’ve used the Set Value to construct the schema in a way I want to display on a page, but I think in your case as your schema is returning fine a repeat with insert or multi-insert would work on it so you can insert each row independently.

So
API Action
Core > Repeat
Within the repeat Action > insert or multi-insert

Reason being is you have multiple rows of data from the same data source that you need to repeat through and insert in one action.

I’d assume someone else will jump in for the correct Wappler way, but from what I understand this would be the optimum way to do so.

1 Like

I’ll give it a proper go today. See if can get it to work. Thanks mate

1 Like

Wow. Working with APIs a bit recently and Wappler has just really impressed me… (thanks @George @Teodor and @patrick .

Apart from figuring out how to work with APIs, schemas, JSON etc, it has been really easy and Wappler has been able to just WORK. Creating complex JSON structures just with the right combination of SC actions. Easy.

And now, after a TINY bit of playing around, it was super simple to get this to work and automatically insert all the records I got from this API into my DB. The trick (thanks @mgaussie) was just to use the API as a repeat…simple.

image

1 Like

Hi @Philip_J
as @mgaussie explained that’s exactly how you handle data manipulation of multiple records.
That’s why the repeat component is used. It can be useful for looping through records and do inserts/update or looping through multiple files on upload and manipulate them, also for looping through CSV import etc.

Of course… don’t get me wrong, I’m very familiar with Repeats :slight_smile:

I just wasn’t seeing how to use the API schema for values/inputs. I’m not sure if I didn’t look properly at first, or if the schema hadn’t been properly defined, but when i went back to it today (using a repeat) it was all so simple.

1 Like