Answer: Import JSON Data
Understand how to import line-delimited JSON data into a staging table using LOAD DATA INFILE, extract JSON fields for updating supplier records, and validate the results. This lesson covers JSON functions, conditional updates, and alternate parsing methods to handle JSON imports safely and effectively.
We'll cover the following...
Solution
The solution is given below:
Explanation
The explanation of the solution code is given below:
Lines 1–3: A staging table
SupplierKpi_Stageis created if it does not already exist.The table contains a single
JSONcolumn namedRawDocto store each raw JSON document as received.Using a staging table isolates the import process from the main
Supplierstable and allows safe parsing and validation.
Lines 5–9: The
LOAD DATA INFILEstatement imports the JSONL file into the staging table.Each line of the file represents one JSON document.
LINES TERMINATED BY '\n'ensures that each line is treated as a separate record.The raw line is captured in the user variable
@jsonand then cast to validJSONbefore being stored inRawDoc.
Lines 11–20: An
UPDATE ... JOINstatement applies the imported KPI values to theSupplierstable.The join condition matches suppliers using the email address extracted from the JSON document with
JSON_EXTRACTandJSON_UNQUOTE. The join condition also removes non-breaking space characters that ...