convert json output to new std

This commit is contained in:
Paul Trowbridge 2025-08-05 17:29:50 -04:00
parent ccb91c87d4
commit 533bbd1046
4 changed files with 272 additions and 101 deletions

60
new_targets/dev.json Normal file
View File

@ -0,0 +1,60 @@
{
"details": [
{
"label": "History",
"details": [
{
"label": "Last Sale: 2025-06-12",
"value": 0.10120,
"type": "currency",
"note": "Ord# 1008338"
}
]
},
{
"label": "List",
"details": [
{
"label": "List:GUAU",
"value": 0.11000,
"type": "currency",
"note": "U.AEA.DI"
}
]
},
{
"label": "Target Support",
"details": [
{
"label": "Anchor:XNS0T1G3",
"value": 0.08000,
"type": "currency",
"note": "+"
},
{
"label": "Channel:WHS",
"value": 0.20000,
"type": "percentage",
"note": "x"
},
{
"label": "Volume:1-8",
"value": 0.10000,
"type": "percentage",
"note": "x"
}
]
},
{
"label": "Guidance",
"details": [
{
"label": "Last Price Capped",
"value": 0.10120,
"type": "currency",
"note": "Last price 0.10120 capped at 0.0000000000000%"
}
]
}
]
}

View File

@ -33,7 +33,7 @@ BEGIN
guidance_price NUMERIC(20,5),
guidance_reason NVARCHAR(MAX),
expl NVARCHAR(MAX),
expl_pretty NVARCHAR(MAX)
ui_json NVARCHAR(MAX)
);
--------------------------------------------------------------------------------
@ -229,40 +229,88 @@ BEGIN
-- Step 7: Clean up for UI
--------------------------------------------------------------------------------
UPDATE q
SET expl_pretty =
(
SET ui_json = (
SELECT
'Target Price' AS [target_price.label],
q.tprice AS [target_price.value],
'Volume Range' AS [volume_range.label],
q.volume_range AS [volume_range.value],
'Channel' AS [channel.label],
q.chan AS [channel.value],
'Tier' AS [tier.label],
q.tier AS [tier.value],
'Last Price' AS [last_price.label],
q.last_price AS [last_price.value],
'Last Date' AS [last_date.label],
q.last_date AS [last_date.value],
'Last Order' AS [last_order.label],
q.last_order AS [last_order.value],
'List Price' AS [list_price.label],
q.listprice AS [list_price.value],
'List Code' AS [list_code.label],
q.listcode AS [list_code.value],
'Guidance Reason' AS [guidance_reason.label],
q.guidance_reason AS [guidance_reason.value],
'Guidance Price' AS [guidance_price.label],
q.guidance_price AS [guidance_price.value]
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
(
SELECT
panel.label,
JSON_QUERY(panel.details) AS details
FROM (
-- History Panel
SELECT
'History' AS label,
(
SELECT
CASE
WHEN q.last_price IS NOT NULL THEN 'Last Sale: ' + CAST(q.last_date AS varchar(10))
ELSE 'No Recent'
END AS label,
COALESCE(q.last_price,0) AS value,
CASE
WHEN q.last_price IS NOT NULL THEN 'currency'
ELSE 'currency'
END AS type,
CASE
WHEN q.last_price IS NOT NULL THEN 'Ord# ' + q.last_order
ELSE NULL
END AS note
FOR JSON PATH
) AS details
UNION ALL
-- List Panel
SELECT
'List' AS label,
(
SELECT
'List:' + q.listcode AS label,
q.listprice AS value,
'currency' AS type,
q.plevel AS note
FOR JSON PATH
)
UNION ALL
-- Target Support Panel
SELECT
'Target Support' AS label,
(
SELECT
RTRIM(SUBSTRING(value,1,18)) AS label,
TRY_CAST(SUBSTRING(value,23,7) AS NUMERIC(20,5))
+ CASE SUBSTRING(value,19,1) WHEN '+' THEN 0 ELSE -1 END AS value,
CASE SUBSTRING(value,19,1) WHEN '+' THEN 'currency' ELSE 'percentage' END AS type,
CASE SUBSTRING(value,19,1) WHEN '+' THEN 'Price' ELSE 'Premium' END AS note
FROM OPENJSON(q.expl, '$.target_math')
WITH (value NVARCHAR(MAX) '$')
FOR JSON PATH
) AS details
UNION ALL
-- Guidance Panel
SELECT
'Guidance' AS label,
(
SELECT
'Price' AS label,
q.guidance_price AS value,
'currency' AS type,
q.guidance_reason AS note
FOR JSON PATH
)
) AS panel
FOR JSON PATH
) AS details,
JSON_QUERY(q.expl) AS data -- 👈 adds the full expl content as a JSON object
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER -- 👈 make it a single JSON object
)
FROM @queue q;
--------------------------------------------------------------------------------
-- Final: Return the enriched result row
--------------------------------------------------------------------------------

View File

@ -228,77 +228,77 @@ BEGIN
--------------------------------------------------------------------------------
-- Step 7: Clean up for UI
--------------------------------------------------------------------------------
UPDATE q
SET ui_json = (
SELECT
panel.label,
JSON_QUERY(panel.details) AS details
FROM (
-- History Panel
SELECT
'History' AS label,
(
SELECT
'Last Sale: ' + CAST(q.last_date AS varchar(10)) AS label,
q.last_price AS value,
'currency' AS type,
'Ord# ' + q.last_order AS note
FOR JSON PATH
) AS details
UPDATE q
SET ui_json = (
SELECT
panel.label,
JSON_QUERY(panel.details) AS details
FROM (
-- History Panel
SELECT
'History' AS label,
(
SELECT
'Last Sale: ' + CAST(q.last_date AS varchar(10)) AS label,
q.last_price AS value,
'currency' AS type,
'Ord# ' + q.last_order AS note
FOR JSON PATH
) AS details
UNION ALL
UNION ALL
-- List Panel
SELECT
'List' AS label,
(
SELECT
'List:' + q.listcode AS label,
q.listprice AS value,
'currency' AS type,
q.plevel AS note
FOR JSON PATH
)
-- List Panel
SELECT
'List' AS label,
(
SELECT
'List:' + q.listcode AS label,
q.listprice AS value,
'currency' AS type,
q.plevel AS note
FOR JSON PATH
)
UNION ALL
UNION ALL
-- Target Support Panel
SELECT
'Target Support' AS label,
(
SELECT
-- parse each item in target_math
RTRIM(SUBSTRING(value,1,18)) label,
TRY_CAST(SUBSTRING(value,23,7) AS NUMERIC(20,5)) + CASE SUBSTRING(value,19,1) WHEN '+' THEN 0 ELSE -1 END AS value,
CASE SUBSTRING(value,19,1) WHEN '+' THEN 'currency' ELSE 'percentage' END AS type,
SUBSTRING(value,19,1) AS note
FROM OPENJSON(q.expl, '$.target_math')
WITH (value NVARCHAR(MAX) '$')
FOR JSON PATH
) AS details
-- Target Support Panel
SELECT
'Target Support' AS label,
(
SELECT
-- parse each item in target_math
RTRIM(SUBSTRING(value,1,18)) label,
TRY_CAST(SUBSTRING(value,23,7) AS NUMERIC(20,5)) + CASE SUBSTRING(value,19,1) WHEN '+' THEN 0 ELSE -1 END AS value,
CASE SUBSTRING(value,19,1) WHEN '+' THEN 'currency' ELSE 'percentage' END AS type,
SUBSTRING(value,19,1) AS note
FROM OPENJSON(q.expl, '$.target_math')
WITH (value NVARCHAR(MAX) '$')
FOR JSON PATH
) AS details
UNION ALL
UNION ALL
-- Guidance Panel
SELECT
'Guidance' AS label,
(
SELECT
'Last Price Capped' AS label,
q.guidance_price AS value,
'currency' AS type,
CONCAT(
'Last price ', q.last_price,
' capped at ',
ROUND((q.last_price - q.guidance_price) / NULLIF(q.last_price, 0) * 100, 2),
'%'
) AS note
FOR JSON PATH
)
) AS panel
FOR JSON PATH, ROOT('details')
)
FROM @queue q;
-- Guidance Panel
SELECT
'Guidance' AS label,
(
SELECT
'Last Price Capped' AS label,
q.guidance_price AS value,
'currency' AS type,
CONCAT(
'Last price ', q.last_price,
' capped at ',
ROUND((q.last_price - q.guidance_price) / NULLIF(q.last_price, 0) * 100, 2),
'%'
) AS note
FOR JSON PATH
)
) AS panel
FOR JSON PATH, ROOT('details')
)
FROM @queue q;
--------------------------------------------------------------------------------

View File

@ -0,0 +1,63 @@
{
"details": [
{
"label": "History",
"details": [
{
"label": "Last Sale",
"value": 0.081,
"type": "currency"
},
{
"label": "Last Quote",
"value": 0.079,
"type": "currency"
}
]
},
{
"label": "List",
"details": [
{
"label": "Code",
"value": "GUAU",
"type": "text",
"note": "Griffin East - US A List"
}
]
},
{
"label": "Target Support",
"details": [
{
"label": "Anchor",
"value": 0.080,
"type": "currency",
"note": "Tier 1 Truckload Black"
},
{
"label": "Color :L",
"value": 0.10,
"type": "percent",
"note": " x "
},
{
"label": "Tier 2",
"value": 0.02,
"type": "percent",
"note": " x "
}
]
},
{
"label": "Guidance",
"details": [
{
"label": "Last Price Capped",
"value": 0.080,
"type": "Last price 0.079 capped at 1%"
}
]
}
]
}