By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
CREATE TABLE test (id INT, json_value JSON);
INSERT INTO test VALUES
(1, '{ "2": { "model": "Test Model" }, "3": { "model": "Test Model" } }'),
(2, '{ "4": { "model": "Test Model" }, "2": { "model": "Test Model" } }'),
(3, '{ "1": { "model": "Test Model" }, "4": { "model": "Test Model" } }'),
(4, '{ "2": { "model": "Test Model" } }');
Records: 4 Duplicates: 0 Warnings: 0
SELECT jsontable.`key`, COUNT(*) `count`
FROM test
CROSS JOIN JSON_TABLE(JSON_KEYS(test.json_value),
'$[*]' COLUMNS (`key` INT PATH '$')) jsontable
GROUP BY jsontable.`key`
key | count |
---|---|
2 | 3 |
3 | 1 |
4 | 2 |
1 | 1 |