You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This change adds an explanation of how to handle an array of JSON objects within an RPC call. To pass multiple objects, an array of JSON objects must be the JSON value, with the key being the json or jsonb variable name of the Postgres function.
For people who want to perform multiple tasks/inserts/updates within a single API call, this is a needed explanation for that use-case.
Copy file name to clipboardExpand all lines: docs/references/api/functions.rst
+34Lines changed: 34 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -71,6 +71,40 @@ The function parameter names match the JSON object keys in the POST case, for th
71
71
72
72
.. _function_single_json:
73
73
74
+
Functions with an array of JSON objects
75
+
----------------------------------------------
76
+
77
+
If you want to pass multiple JSON objects to a Postgres function (an array of objects), you can create a function with a parameter of type ``json`` or ``jsonb``.
78
+
79
+
Within the curl request, this JSON must be embedded in an object where they key matches the same name as the function's ``json`` or ``jsonb`` parameter.
80
+
This will allow you to loop over the array of JSON objects within the Postgres function.
81
+
82
+
This practice may allow you to reduce the number of ``curl`` requests required to accomplish a task.
83
+
84
+
For instance, assume we have created this function in the database.
85
+
86
+
.. code-block:: postgres
87
+
88
+
CREATE FUNCTION update_data(p_json jsonb)
89
+
RETURNS void AS $$
90
+
DECLARE
91
+
json_item json;
92
+
BEGIN
93
+
FOR json_item IN SELECT jsonb_array_elements(p_json) LOOP
94
+
UPDATE data_table SET data_text_column = (json_item->>'data_text')::text
95
+
WHERE data_int_column = (json_item->>'data_int')::integer;
96
+
END LOOP;
97
+
END;
98
+
$$ LANGUAGE SQL IMMUTABLE;
99
+
100
+
A ``curl`` request using the POST method would look like the following:
0 commit comments