![]() Updating Multiple Rows Using a JSON Array Data This is how, with the help of json_populate_recordset, you can insert multiple rows in your table with JSON data. The json_populate_recordset receives 2 parameters: one being the JSON data type, users in this case, and secondly, the JSON array data in string format. We are stringifying the JSON data to convert it into string using JSON.stringify(usersJsonArrayData) to pass it into the SQL query. In the above query, we are inserting a JSON array data stored in a variable called usersJsonArrayData. And, of course, the operators needs to be removed from the final query. Note: Whenever I am using, that means either it's some variable defined in the code or it's some kind of custom statement (like, for example, a comparison statement) which needs to be modified as per your own use case. INSERT INTO users(name, email, mobile, username) SELECT name, email, mobile, username FROM json_populate_recordset(null::users, '') ![]() Let's see how to insert some rows into, say, a users table that contains fields like name, email, mobile, username, etc. Inserting Multiple-Row Data Using a JSON Array PostgreSQL brings us some amazing functions that can be used to manipulate data in our database directly using either the JSON object (for a single row) or JSON array (for multiple rows) from an API response, for example. Gone are the days when we were used to creating queries manually for inserting multiple rows in SQL.
0 Comments
Leave a Reply. |