SQL PROCEDURE: getAsJSON(schema,name,where)


So there doesn’t seem to be a convenient way to retrieve an asterisk of a table to a JSON in mySQL natively so this simple procedure does the job for me.

CREATE PROCEDURE `getAsJSON` (
IN schemaName VARCHAR(255),
IN tableName VARCHAR(255),
IN whereLine VARCHAR(255)
)
BEGIN
select concat('select json_object(',
    group_concat(concat(quote(column_name), ', ', column_name)), 
    ') as JSON from ',schemaName,'.',tableName,' ',whereLine,';') into @sql 
  from information_schema.columns 
  where table_schema = schemaName and table_name = tableName;
  select @sql;
prepare stmt from @sql;
execute stmt;
END

Execution is simple.

CALL getAsJSON('FIS_Calendar','A_event','ORDER BY Eventid DESC LIMIT 1')
{"Eventid": 52658, "Seasoncode": 2023, "Sectorcode": "AL", "Eventname": "UAE PARA Alpine Competition - Asia Cup", "Startdate": "2022-11-06", "Enddate": "2022-11-07", "Nationcodeplace": "UAE", "Orgnationcode": "UAE", "Place": "Ski Dubai, Mall of the Emirates", "Published": 1, "OrgaddressL1": "UAE Winter Sports Federation", "OrgaddressL2": "Ski Dubai, Mall of the Emirates", "OrgaddressL3": "", "OrgaddressL4": "Al Barsha 1, Dubai", "Orgtel": "+971556659111", "Orgmobile": "", "Orgfax": "", "OrgEmail": "Ski@wsfuae.com", "Orgemailentries": "Ski@wsfuae.com", "OrgWebsite": "http://", "Socialmedia": "", "Eventnotes": "", "Languageused": "", "Selcat": "-PARA-", "Seldis": "-SL-", "Seldisl": "2xSL", "Seldism": "SL", "Dispdate": "6.-7.11.2022", "Discomment": "", "Version": 0, "Placeid": 0, "Lastupdate": "2022-09-05 06:59:59"}