Como dije, puede ser un desafío analizar JSON en MySQL ya que aquí se trata de claves basadas en texto.
Entonces necesitaría usar JSON_KEYS()
para obtenerlos en combinación con un generador de números, se genera una ruta JSON dinámica para usar en JSON_EXTRACT()
Función 8 de MySQL JSON_TABLE()
hace que sea mucho más fácil..
Consulta
SELECT
JSON_UNQUOTE(
JSON_EXTRACT(json , CONCAT('$.', SUBSTRING_INDEX(
SUBSTRING_INDEX(json_parsed, ',', number_generator.number)
, ','
, -1
), '.name'))) AS name
FROM (
SELECT
@row := @row + 1 AS number
FROM (
SELECT 0 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9
) row1
CROSS JOIN (
SELECT 0 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9
) row2
CROSS JOIN (
SELECT @row := 0
) init_user_params
) AS number_generator
CROSS JOIN (
SELECT
SUBSTRING(json_keys, 2, json_keys_length - 2) AS json_parsed
, json_keys
, json
, JSON_LENGTH(json_keys) AS json_array_length
FROM (
SELECT
JSON_KEYS(record.json) AS json_keys
, json
, LENGTH(JSON_KEYS(record.json)) AS json_keys_length
FROM (
SELECT
'{
"Intitule": {
"name": "Intitule de la formation",
"stats": false,
"is_array": false,
"is_filter": true,
"chart": "pie",
"col": "6"
},
"Fin": {
"name": "Date de fin",
"stats": false,
"is_array": false,
"is_filter": false,
"chart": "pie",
"col": "6"
}
}' AS json
FROM
DUAL
) AS record
) AS json_information
) AS json_init
WHERE
number_generator.number BETWEEN 0 AND json_array_length
AND
JSON_EXTRACT(json , CONCAT('$.', SUBSTRING_INDEX(
SUBSTRING_INDEX(json_parsed, ',', number_generator.number)
, ','
, -1
), '.is_filter')) = true
Resultado
| name |
| ------------------------ |
| Intitule de la formation |
ver demostración