Reputation: 1
Is there a function or method available to obtain a response in the form of an object key-value pair? I've currently achieved this by concatenating, but it's time-consuming for large datasets. Additionally, the input may consist of over a 5000 CRN numbers at once.
Expected Output:
{crn_no:{associated data with that no}}
eg: {
"20035A": {
"MOVEMENT": "REGISTRATION",
"REASON": "DISMISSAL",
"STATUS": "CLOSED",
},
"220022F": {
"MOVEMENT": "REGISTRATION",
"REASON": null,
"STATUS": "OPEN",
}
}
I Tried several methods but was not able so did it with concatenating which is generating in string format and consumes a lot of time. Is there any other approach known to anybody which can achieve the output. Please assist.
NOT GETTING result as Expected:
WITH crn_data AS (
SELECT crn_number,
STATUS,
MOVEMENT,
STATUS,
FROM
table.view_name
WHERE
crn_number IN UNNEST(@crnNo)
)
WITH crn_data AS (
SELECT crn_number,
STRUCT(
STATUS,
MOVEMENT,
STATUS
) AS crn_details
FROM table.view_name
WHERE crn_number IN UNNEST(@crnNo)
)
SELECT TO_JSON_STRING(
(SELECT AS STRUCT crn_number, claim_details FROM crn_data)
) AS json_output;
Concatenating working query:
WITH crn_data AS (
SELECT
crn_number,
TO_JSON_STRING(
STRUCT(
STATUS,
REASON,
STATUS,
)
) AS json_data
FROM
table.view_name
WHERE
crn_number IN UNNEST(@crnNo)
)
SELECT CONCAT("{", STRING_AGG(CONCAT('"', crn_number, '":', json_data), ","), "}") AS result
FROM crn_data;
Upvotes: 0
Views: 29