Im converting a JSON file into columns, but this one column I’m not able to read it, it shows null not the value.
CREATE OR REPLACE TEMPORARY TABLE SAMPLE_J
(
SAMPLE_JSON VARCHAR(100)
);
INSERT INTO SAMPLE_J
(SAMPLE_JSON)
VALUES ('{"ident":["Product:Tires","sender_id:12345","sender_name:John Who","Tire_Condition:New"]}'
--this is my query
SELECT
SAMPLE_JSON,
JSON_EXTRACT_PATH_TEXT(SAMPLE_JSON,'Product') as test1,
JSON_EXTRACT_PATH_TEXT(SAMPLE_JSON,'Product[0].Product') as test2
FROM SAMPLE_J;
the result is null for test 1 and test 2
This is how JSON file Looks:
{
"ident": [
"Product:Tires",
"sender_id:12345",
"sender_name:John Who",
"Tire_Condition:New"
]
}
2
Answers
What you have here is an array inside the key
ident
.j_ident0
is the first element of that array, and its value isProduct:Tires
. That’s not a JSON, just a string.And expression of Felipe’s answer, showing accessing the JSON if it has already been converted to JSON/Variant via PARSE_JSON, and then accessing via the inline methods is:
but looking at that data, it’s not in of itself, an JSON object, but an array of strings, and that implies there is a chance the order of the data might be variable.
So that array itself might be safer to process via a FLATTEN on the array, and taking just the part with the desired prefix:
thus using a FLATTEN and a WHERE:
we get: