When iterating on a JSON objects, there is currently no way to retrieve the key allowing to access to the current object.
As an example:
SELECT jt.*
FROM JSON_TABLE(
'{"a": 41, "b": 42, "c": 43}', '$.*'
COLUMNS (v INT(11) path '$', i FOR ORDINALITY)) AS jt
Will produce:
v
i
41
1
42
2
43
3
But there is no way to retrieve:
v
i
k
41
1
a
42
2
b
43
3
c
The proposal is to add, as for "FOR ORDINALITY", the "FOR KEY" clause that would allow to retrieve the key when doing:
SELECT jt.*
FROM JSON_TABLE(
'{"a": 41, "b": 42, "c": 43}', '$.*'
COLUMNS (
v INT(11) path '$',
i FOR ORDINALITY,
k VARCHAR(5) FORKEY
)) AS jt
Final Implementation:
Implementing keyvalue() function as part of the path can become complicated. Because keyvalue() will "transform" an object. Now, having this keyvalue() in path means the path will "return" something instead of only leading to some character in the json document. Also, there are few more "path functions"in addition to keyvalue() so supporting function in path qualifies for a completely new task.
As an alternative though, we can have a separate json function (JSON_KEY_VALUE(<json_doc>, <path>)) which transforms an object into key-value pairs and have the result that this function returns as an argument in JSON_TABLE().
Like so:
JSON_TABLE ( JSON_KEY_VALUE(<json_doc>, <path>) , PATH COLUMN ... ) .
Another reason to rather have a function:
-Can be used else where if needed and not restricted to path.
It doesn't introduce a new non-standard syntax and it is consistent with what we're doing already ( Example there is also type() function in standards, and we have JSON_TYPE() as equivalent).
Attachments
Issue Links
is duplicated by
MDEV-30494JSON_TABLE - reference to object keys from PATH '$.*' is not possible or documented
Closed
relates to
MDEV-31477Inconsistent handling while fetching values in json
I suspect the standard solution would be to use keyvalue function, like (completely untested)
SELECT jt.*
FROM JSON_TABLE(
'{"a": 41, "b": 42, "c": 43}', '$.keyvalue()[*]'
COLUMNS (v INT(11) path '$.value', i FOR ORDINALITY, k VARCHAR(5) path '$.key')) AS jt
which is something we don't support yet
Sergei Golubchik
added a comment - I suspect the standard solution would be to use keyvalue function, like (completely untested)
SELECT jt.*
FROM JSON_TABLE(
'{"a": 41, "b": 42, "c": 43}' , '$.keyvalue()[*]'
COLUMNS (v INT (11) path '$.value' , i FOR ORDINALITY, k VARCHAR (5) path '$.key' )) AS jt
which is something we don't support yet
After discussing with HF, decided that implementing keyvalue() function as part of the path can become complicated. Because keyvalue() will transform an object, which means the path will "return" something instead of only leading to some character in the json. Also, we more functions in addition to keyvalue() so supporting function in path qualifies for a completely new task.
As an alternative though, we can have a separate json function (JSON_KEY_VALUE(<json_doc>, <path>)) which transforms an object into key-value pairs. And we can have the result that this function returns, as an argument in JSON_TABLE().
Something like this:
JSON_TABLE ( JSON_KEY_VALUE(<json_doc>, <path>) , PATH COLUMN ... ) .
Also this function can be used else where if needed and not restricted to path. Having what the description says ( key as a separate column, like key_list FOR KEY ) will also restrict it to json table. Let me know what you think.
Sergei approved. Because it doesn't introduce a new non-standard syntax and it is consistent with what we're doing already (There is also type() function in standards, and we have JSON_TYPE() as equivalent).
Rucha Deodhar
added a comment - After discussing with HF, decided that implementing keyvalue() function as part of the path can become complicated. Because keyvalue() will transform an object, which means the path will "return" something instead of only leading to some character in the json. Also, we more functions in addition to keyvalue() so supporting function in path qualifies for a completely new task.
As an alternative though, we can have a separate json function (JSON_KEY_VALUE(<json_doc>, <path>)) which transforms an object into key-value pairs. And we can have the result that this function returns, as an argument in JSON_TABLE().
Something like this:
JSON_TABLE ( JSON_KEY_VALUE(<json_doc>, <path>) , PATH COLUMN ... ) .
Also this function can be used else where if needed and not restricted to path. Having what the description says ( key as a separate column, like key_list FOR KEY ) will also restrict it to json table. Let me know what you think.
Sergei approved. Because it doesn't introduce a new non-standard syntax and it is consistent with what we're doing already (There is also type() function in standards, and we have JSON_TYPE() as equivalent).
When iterating on a JSON objects, there is currently no way to retrieve the key allowing to access to the current object.
As an example:
{code:sql}
SELECT jt.*
FROM JSON_TABLE(
'{"a": 41, "b": 42, "c": 43}', '$.*'
COLUMNS (v INT(11) path '$', i FOR ORDINALITY)) AS jt
{code}
Will produce:
|| v || i ||
| 41| 1 |
| 42| 2 |
| 43| 3 |
But there is no way to retrieve:
|| v || i || k ||
| 41| 1 | a |
| 42| 2 | b |
| 43| 3 | c |
The proposal is to add, as for "FOR ORDINALITY", the "FOR KEY" clause that would allow to retrieve the key when doing:
{code:sql}
SELECT jt.*
FROM JSON_TABLE(
'{"a": 41, "b": 42, "c": 43}', '$.*'
COLUMNS (
v INT(11) path '$',
i FOR ORDINALITY,
k VARCHAR(5) FOR KEY
)) AS jt
{code}
When iterating on a JSON objects, there is currently no way to retrieve the key allowing to access to the current object.
As an example:
{code:sql}
SELECT jt.*
FROM JSON_TABLE(
'{"a": 41, "b": 42, "c": 43}', '$.*'
COLUMNS (v INT(11) path '$', i FOR ORDINALITY)) AS jt
{code}
Will produce:
|| v || i ||
| 41| 1 |
| 42| 2 |
| 43| 3 |
But there is no way to retrieve:
|| v || i || k ||
| 41| 1 | a |
| 42| 2 | b |
| 43| 3 | c |
The proposal is to add, as for "FOR ORDINALITY", the "FOR KEY" clause that would allow to retrieve the key when doing:
{code:sql}
SELECT jt.*
FROM JSON_TABLE(
'{"a": 41, "b": 42, "c": 43}', '$.*'
COLUMNS (
v INT(11) path '$',
i FOR ORDINALITY,
k VARCHAR(5) FOR KEY
)) AS jt
{code}
Final Implementation:
Implementing keyvalue() function as part of the _path_ can become complicated. Because keyvalue() will "transform" an object. Now, having this keyvalue() in path means the path will "return" something instead of only leading to some character in the json document. Also, there are few more "path functions"in addition to keyvalue() so supporting function in path qualifies for a completely new task.
As an alternative though, we can have a separate json function (JSON_KEY_VALUE(<json_doc>, <path>)) which transforms an object into key-value pairs and have the result that this function returns as an argument in JSON_TABLE().
Like so:
JSON_TABLE ( JSON_KEY_VALUE(<json_doc>, <path>) , PATH COLUMN ... ) .
Another reason to rather have a function:
-Can be used else where if needed and not restricted to path.
- It doesn't introduce a new non-standard syntax and it is consistent with what we're doing already ( Example there is also type() function in standards, and we have JSON_TYPE() as equivalent).
People
Rucha Deodhar
Christian Proust
Votes:
1Vote for this issue
Watchers:
6Start watching this issue
Dates
Created:
Updated:
Resolved:
Git Integration
Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.
{"report":{"fcp":1469.5,"ttfb":724.3999996185303,"pageVisibility":"visible","entityId":117386,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":32,"apdex":0.5,"journeyId":"231bddb3-9345-4dbf-a304-13db1b59c671","navigationType":0,"readyForUser":1539.7999997138977,"redirectCount":0,"resourceLoadedEnd":1162.7999997138977,"resourceLoadedStart":730.3999996185303,"resourceTiming":[{"duration":41.200000286102295,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":730.3999996185303,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":730.3999996185303,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":771.5999999046326,"responseStart":0,"secureConnectionStart":0},{"duration":41.19999980926514,"initiatorType":"link","name":"https://jira.mariadb.org/s/7ebd35e77e471bc30ff0eba799ebc151-CDN/lu2cib/820016/12ta74/494e4c556ecbb29f90a3d3b4f09cb99c/_/download/contextbatch/css/jira.browse.project,project.issue.navigator,jira.view.issue,jira.general,jira.global,atl.general,-_super/batch.css?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&slack-enabled=true&whisper-enabled=true","startTime":730.6999998092651,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":730.6999998092651,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":771.8999996185303,"responseStart":0,"secureConnectionStart":0},{"duration":301.90000009536743,"initiatorType":"script","name":"https://jira.mariadb.org/s/0917945aaa57108d00c5076fea35e069-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":730.7999997138977,"connectEnd":730.7999997138977,"connectStart":730.7999997138977,"domainLookupEnd":730.7999997138977,"domainLookupStart":730.7999997138977,"fetchStart":730.7999997138977,"redirectEnd":0,"redirectStart":0,"requestStart":773.0999999046326,"responseEnd":1032.6999998092651,"responseStart":818.7999997138977,"secureConnectionStart":730.7999997138977},{"duration":431.69999980926514,"initiatorType":"script","name":"https://jira.mariadb.org/s/2d8175ec2fa4c816e8023260bd8c1786-CDN/lu2cib/820016/12ta74/494e4c556ecbb29f90a3d3b4f09cb99c/_/download/contextbatch/js/jira.browse.project,project.issue.navigator,jira.view.issue,jira.general,jira.global,atl.general,-_super/batch.js?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&locale=en&slack-enabled=true&whisper-enabled=true","startTime":731.0999999046326,"connectEnd":731.0999999046326,"connectStart":731.0999999046326,"domainLookupEnd":731.0999999046326,"domainLookupStart":731.0999999046326,"fetchStart":731.0999999046326,"redirectEnd":0,"redirectStart":0,"requestStart":772.8999996185303,"responseEnd":1162.7999997138977,"responseStart":813.8999996185303,"secureConnectionStart":731.0999999046326},{"duration":90.90000009536743,"initiatorType":"script","name":"https://jira.mariadb.org/s/a9324d6758d385eb45c462685ad88f1d-CDN/lu2cib/820016/12ta74/c92c0caa9a024ae85b0ebdbed7fb4bd7/_/download/contextbatch/js/atl.global,-_super/batch.js?locale=en","startTime":731.2999997138977,"connectEnd":731.2999997138977,"connectStart":731.2999997138977,"domainLookupEnd":731.2999997138977,"domainLookupStart":731.2999997138977,"fetchStart":731.2999997138977,"redirectEnd":0,"redirectStart":0,"requestStart":773.2999997138977,"responseEnd":822.1999998092651,"responseStart":818,"secureConnectionStart":731.2999997138977},{"duration":96.60000038146973,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-en/jira.webresources:calendar-en.js","startTime":731.3999996185303,"connectEnd":731.3999996185303,"connectStart":731.3999996185303,"domainLookupEnd":731.3999996185303,"domainLookupStart":731.3999996185303,"fetchStart":731.3999996185303,"redirectEnd":0,"redirectStart":0,"requestStart":773.2999997138977,"responseEnd":828,"responseStart":824.7999997138977,"secureConnectionStart":731.3999996185303},{"duration":114.09999990463257,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-localisation-moment/jira.webresources:calendar-localisation-moment.js","startTime":731.5,"connectEnd":731.5,"connectStart":731.5,"domainLookupEnd":731.5,"domainLookupStart":731.5,"fetchStart":731.5,"redirectEnd":0,"redirectStart":0,"requestStart":798.3999996185303,"responseEnd":845.5999999046326,"responseStart":842,"secureConnectionStart":731.5},{"duration":40.90000009536743,"initiatorType":"link","name":"https://jira.mariadb.org/s/b04b06a02d1959df322d9cded3aeecc1-CDN/lu2cib/820016/12ta74/a2ff6aa845ffc9a1d22fe23d9ee791fc/_/download/contextbatch/css/jira.global.look-and-feel,-_super/batch.css","startTime":731.6999998092651,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":731.6999998092651,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":772.5999999046326,"responseStart":0,"secureConnectionStart":0},{"duration":59.799999713897705,"initiatorType":"link","name":"https://jira.mariadb.org/s/3ac36323ba5e4eb0af2aa7ac7211b4bb-CDN/lu2cib/820016/12ta74/d176f0986478cc64f24226b3d20c140d/_/download/contextbatch/css/com.atlassian.jira.projects.sidebar.init,-_super,-project.issue.navigator,-jira.view.issue/batch.css?jira.create.linked.issue=true","startTime":732,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":732,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":791.7999997138977,"responseStart":0,"secureConnectionStart":0},{"duration":113.89999961853027,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":732,"connectEnd":732,"connectStart":732,"domainLookupEnd":732,"domainLookupStart":732,"fetchStart":732,"redirectEnd":0,"redirectStart":0,"requestStart":800.5,"responseEnd":845.8999996185303,"responseStart":842.8999996185303,"secureConnectionStart":732},{"duration":114.59999990463257,"initiatorType":"script","name":"https://jira.mariadb.org/s/5d5e8fe91fbc506585e83ea3b62ccc4b-CDN/lu2cib/820016/12ta74/d176f0986478cc64f24226b3d20c140d/_/download/contextbatch/js/com.atlassian.jira.projects.sidebar.init,-_super,-project.issue.navigator,-jira.view.issue/batch.js?jira.create.linked.issue=true&locale=en","startTime":732.1999998092651,"connectEnd":732.1999998092651,"connectStart":732.1999998092651,"domainLookupEnd":732.1999998092651,"domainLookupStart":732.1999998092651,"fetchStart":732.1999998092651,"redirectEnd":0,"redirectStart":0,"requestStart":801.5,"responseEnd":846.7999997138977,"responseStart":844.5,"secureConnectionStart":732.1999998092651},{"duration":394.30000019073486,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-js/jira.webresources:bigpipe-js.js","startTime":733.1999998092651,"connectEnd":733.1999998092651,"connectStart":733.1999998092651,"domainLookupEnd":733.1999998092651,"domainLookupStart":733.1999998092651,"fetchStart":733.1999998092651,"redirectEnd":0,"redirectStart":0,"requestStart":945,"responseEnd":1127.5,"responseStart":1122.5999999046326,"secureConnectionStart":733.1999998092651},{"duration":407.80000019073486,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-init/jira.webresources:bigpipe-init.js","startTime":737.7999997138977,"connectEnd":737.7999997138977,"connectStart":737.7999997138977,"domainLookupEnd":737.7999997138977,"domainLookupStart":737.7999997138977,"fetchStart":737.7999997138977,"redirectEnd":0,"redirectStart":0,"requestStart":1117.5,"responseEnd":1145.5999999046326,"responseStart":1144,"secureConnectionStart":737.7999997138977},{"duration":229.09999990463257,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":1170.7999997138977,"connectEnd":1170.7999997138977,"connectStart":1170.7999997138977,"domainLookupEnd":1170.7999997138977,"domainLookupStart":1170.7999997138977,"fetchStart":1170.7999997138977,"redirectEnd":0,"redirectStart":0,"requestStart":1367,"responseEnd":1399.8999996185303,"responseStart":1399.0999999046326,"secureConnectionStart":1170.7999997138977}],"fetchStart":0,"domainLookupStart":0,"domainLookupEnd":0,"connectStart":0,"connectEnd":0,"requestStart":87,"responseStart":724,"responseEnd":729,"domLoading":728,"domInteractive":1668,"domContentLoadedEventStart":1668,"domContentLoadedEventEnd":1730,"domComplete":2345,"loadEventStart":2346,"loadEventEnd":2346,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":1617.3999996185303},{"name":"bigPipe.sidebar-id.end","time":1618.1999998092651},{"name":"bigPipe.activity-panel-pipe-id.start","time":1618.2999997138977},{"name":"bigPipe.activity-panel-pipe-id.end","time":1625},{"name":"activityTabFullyLoaded","time":1752.6999998092651}],"measures":[],"correlationId":"ffaa4bafdcaf37","effectiveType":"4g","downlink":9.5,"rtt":0,"serverDuration":569,"dbReadsTimeInMs":33,"dbConnsTimeInMs":44,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}
I suspect the standard solution would be to use keyvalue function, like (completely untested)
which is something we don't support yet