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_price NUMERIC(20,5),
guidance_reason NVARCHAR(MAX), guidance_reason NVARCHAR(MAX),
expl NVARCHAR(MAX), expl NVARCHAR(MAX),
expl_pretty NVARCHAR(MAX) ui_json NVARCHAR(MAX)
); );
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
@ -229,40 +229,88 @@ BEGIN
-- Step 7: Clean up for UI -- Step 7: Clean up for UI
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
UPDATE q UPDATE q
SET expl_pretty = SET ui_json = (
(
SELECT SELECT
'Target Price' AS [target_price.label], (
q.tprice AS [target_price.value], SELECT
'Volume Range' AS [volume_range.label], panel.label,
q.volume_range AS [volume_range.value], JSON_QUERY(panel.details) AS details
'Channel' AS [channel.label], FROM (
q.chan AS [channel.value], -- History Panel
'Tier' AS [tier.label], SELECT
q.tier AS [tier.value], 'History' AS label,
'Last Price' AS [last_price.label], (
q.last_price AS [last_price.value], SELECT
'Last Date' AS [last_date.label], CASE
q.last_date AS [last_date.value], WHEN q.last_price IS NOT NULL THEN 'Last Sale: ' + CAST(q.last_date AS varchar(10))
'Last Order' AS [last_order.label], ELSE 'No Recent'
q.last_order AS [last_order.value], END AS label,
'List Price' AS [list_price.label], COALESCE(q.last_price,0) AS value,
q.listprice AS [list_price.value], CASE
'List Code' AS [list_code.label], WHEN q.last_price IS NOT NULL THEN 'currency'
q.listcode AS [list_code.value], ELSE 'currency'
'Guidance Reason' AS [guidance_reason.label], END AS type,
q.guidance_reason AS [guidance_reason.value], CASE
'Guidance Price' AS [guidance_price.label], WHEN q.last_price IS NOT NULL THEN 'Ord# ' + q.last_order
q.guidance_price AS [guidance_price.value] ELSE NULL
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER 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; FROM @queue q;
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- Final: Return the enriched result row -- Final: Return the enriched result row
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------

View File

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