Home > Mobile >  MySQL: Extract all Keys from a nested JSON String
MySQL: Extract all Keys from a nested JSON String

Time:02-08

In addition to this answer is it possible to extract nested keys in a simple way? Example:

{
  "a": value,
  "b": {
    "c": value
    "d": {
      "e": value
   } 
  }
}

Expected output: ['a', 'b.c', 'b.d.e'] What I have tried:

 SELECT 
 f.`id` AS `field_name`
 
 FROM table t,
    JSON_TABLE(
         JSON_KEYS(t.`column`, '$.b'),
         '$[*]' COLUMNS(
           `id` VARCHAR(191) PATH '$'
         )
     ) AS t

but that would only show me one of the nested keys and skip the outer

CodePudding user response:

SELECT JSON_SEARCH(val, 'all', 'value') result
FROM test;
| result                      |
| :-------------------------- |
| ["$.a", "$.b.c", "$.b.d.e"] |

db<>fiddle here

  •  Tags:  
  • Related