I am trying to return a single order from a SQL Server table as JSON from a stored procedure to create input for an action in a Logic App. The procedure always wraps the result in something more than the simple array of name-value pairs; note: the following examples are from different orders.
I want:
[
{
"order_id": "385604",
"warehouse_id": "1",
"item_warehouses_overridden": "0",
"customer_id": "263",
"customer_po": "Inv- Chad Gracy and Joshua Ruff",
"date": "02/26/2025",
"date_internal": "2025-02-26"...
But I get (@jsonResult
is the output param of the stored procedure):
{
"jsonResult": "{\"order_id\":\"385582\",\"warehouse_id\":\"1\",\"item_warehouses_overridden\":\"0\",\"customer_id\":\"1209\",\"customer_po\":\"Stock Knit Headcovers\",\"date\":\"02\\/26\\/2025\",\"date_internal\":\"2025-02-26\"...
My stored procedure is shown here, I've tried using a Parse JSON action in the logic app, but naturally get a schema error.
ALTER PROCEDURE [dbo].[proc_get_order_by_order_id_JSON]
(
-- Add the parameters for the stored procedure here
@order_num nvarchar(10),
@jsonResult NVARCHAR(MAX) OUTPUT
)
AS
BEGIN
SET NOCOUNT ON
SELECT @jsonResult = (
SELECT
order_id,
warehouse_id,
item_warehouses_overridden,
customer_id,
customer_po,
date,
date_internal
FROM
tbl_orders
WHERE
(order_id = @order_num)
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
);
-- Use JSON_QUERY to avoid escaping quotes
SELECT @jsonResult = JSON_QUERY(@jsonResult);
END;
WITHOUT_ARRAY_WRAPPER
when you do want the wrapper? Why then useSELECT @jsonResult = JSON_QUERY(@jsonResult);
afterwards? If you remove both of those, you get the result you want. db<>fiddledate
column isn't a valid date or even adate
(it's a(n)varchar
); there aren't 26 months in the year.