Extract JSON object's values as array from MySQL JSON column
I have a MySQL column of json type. In there, a dict like JSON object is stored. Now, I want to extract the values from this JSON object and create a JSON array.
How can I achieve this?
Example Query
with json_objs(json_col) as (
select CAST('{"key1": "value1", "key2": "value2"}' AS JSON)
UNION ALL
select CAST('{"key3": "value3", "key4": "value4"}' AS JSON)
)
select SOME_EXPR_I_CAN_T_FIGURE_OUT from json_objs
Expected result
+----------------------+
| resulting_column |
+----------------------+
| ["value1", "value2"] |
| ["value3", "value4"] |
+----------------------+
(If table DDL is desired:)
CREATE TABLE `json_objs` (
`json_col` json DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
mysql arrays json json-extract
add a comment |
I have a MySQL column of json type. In there, a dict like JSON object is stored. Now, I want to extract the values from this JSON object and create a JSON array.
How can I achieve this?
Example Query
with json_objs(json_col) as (
select CAST('{"key1": "value1", "key2": "value2"}' AS JSON)
UNION ALL
select CAST('{"key3": "value3", "key4": "value4"}' AS JSON)
)
select SOME_EXPR_I_CAN_T_FIGURE_OUT from json_objs
Expected result
+----------------------+
| resulting_column |
+----------------------+
| ["value1", "value2"] |
| ["value3", "value4"] |
+----------------------+
(If table DDL is desired:)
CREATE TABLE `json_objs` (
`json_col` json DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
mysql arrays json json-extract
Does mysqlserverteam.com/mysql-8-0-labs-json-aggregation-functions help?
– Nico Haase
Nov 28 '18 at 9:39
Can you post the SHOW CREATE TABLE result?
– marekful
Nov 28 '18 at 9:44
Are your key names all different or repeating?
– marekful
Nov 28 '18 at 9:54
add a comment |
I have a MySQL column of json type. In there, a dict like JSON object is stored. Now, I want to extract the values from this JSON object and create a JSON array.
How can I achieve this?
Example Query
with json_objs(json_col) as (
select CAST('{"key1": "value1", "key2": "value2"}' AS JSON)
UNION ALL
select CAST('{"key3": "value3", "key4": "value4"}' AS JSON)
)
select SOME_EXPR_I_CAN_T_FIGURE_OUT from json_objs
Expected result
+----------------------+
| resulting_column |
+----------------------+
| ["value1", "value2"] |
| ["value3", "value4"] |
+----------------------+
(If table DDL is desired:)
CREATE TABLE `json_objs` (
`json_col` json DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
mysql arrays json json-extract
I have a MySQL column of json type. In there, a dict like JSON object is stored. Now, I want to extract the values from this JSON object and create a JSON array.
How can I achieve this?
Example Query
with json_objs(json_col) as (
select CAST('{"key1": "value1", "key2": "value2"}' AS JSON)
UNION ALL
select CAST('{"key3": "value3", "key4": "value4"}' AS JSON)
)
select SOME_EXPR_I_CAN_T_FIGURE_OUT from json_objs
Expected result
+----------------------+
| resulting_column |
+----------------------+
| ["value1", "value2"] |
| ["value3", "value4"] |
+----------------------+
(If table DDL is desired:)
CREATE TABLE `json_objs` (
`json_col` json DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
mysql arrays json json-extract
mysql arrays json json-extract
edited Nov 28 '18 at 10:13
marekful
10.1k42143
10.1k42143
asked Nov 28 '18 at 9:29
Yuki InoueYuki Inoue
53211026
53211026
Does mysqlserverteam.com/mysql-8-0-labs-json-aggregation-functions help?
– Nico Haase
Nov 28 '18 at 9:39
Can you post the SHOW CREATE TABLE result?
– marekful
Nov 28 '18 at 9:44
Are your key names all different or repeating?
– marekful
Nov 28 '18 at 9:54
add a comment |
Does mysqlserverteam.com/mysql-8-0-labs-json-aggregation-functions help?
– Nico Haase
Nov 28 '18 at 9:39
Can you post the SHOW CREATE TABLE result?
– marekful
Nov 28 '18 at 9:44
Are your key names all different or repeating?
– marekful
Nov 28 '18 at 9:54
Does mysqlserverteam.com/mysql-8-0-labs-json-aggregation-functions help?
– Nico Haase
Nov 28 '18 at 9:39
Does mysqlserverteam.com/mysql-8-0-labs-json-aggregation-functions help?
– Nico Haase
Nov 28 '18 at 9:39
Can you post the SHOW CREATE TABLE result?
– marekful
Nov 28 '18 at 9:44
Can you post the SHOW CREATE TABLE result?
– marekful
Nov 28 '18 at 9:44
Are your key names all different or repeating?
– marekful
Nov 28 '18 at 9:54
Are your key names all different or repeating?
– marekful
Nov 28 '18 at 9:54
add a comment |
2 Answers
2
active
oldest
votes
You can use the ->
operator as in the expression column -> path
as follows:
create table table1 (
json_dict JSON
);
insert into table1 values('{"ak":"av","bk":"bv"}');
insert into table1 values('{"ak2":"av2","bk2":"bv2"}');
select * from table1;
+------------------------------+
| json_dict |
+------------------------------+
| {"ak": "av", "bk": "bv"} |
| {"ak2": "av2", "bk2": "bv2"} |
+------------------------------+
2 rows in set (0.00 sec)
select json_dict->"$.*" from table1;
+------------------+
| json_dict->"$.*" |
+------------------+
| ["av", "bv"] |
| ["av2", "bv2"] |
+------------------+
2 rows in set (0.00 sec)
https://dev.mysql.com/doc/refman/8.0/en/json-search-functions.html#operator_json-column-path
1
Thanks for the fiddle. also +1 to answer :)
– Madhur Bhaiya
Nov 28 '18 at 10:12
add a comment |
Other than ->
operator, you can also use the Json_Extract()
function:
Schema (MySQL v8.0)
create table table1 (
json_dict JSON
);
insert into table1 values('{"ak":"av","bk":"bv"}');
insert into table1 values('{"ak2":"av2","bk2":"bv2"}');
Query #1
select JSON_EXTRACT(json_dict, '$.*') from table1;
| JSON_EXTRACT(json_dict, '$.*') |
| ------------------------------ |
| ["av", "bv"] |
| ["av2", "bv2"] |
View on DB Fiddle
I was faster and you copied code from my answer. However, I upvoted yours because you showed a slightly different alternative :)
– marekful
Nov 28 '18 at 10:10
@marekful Thanks for the fiddle. I was lazy to create a fiddle; so pouched on your fiddle :P
– Madhur Bhaiya
Nov 28 '18 at 10:11
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53516167%2fextract-json-objects-values-as-array-from-mysql-json-column%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use the ->
operator as in the expression column -> path
as follows:
create table table1 (
json_dict JSON
);
insert into table1 values('{"ak":"av","bk":"bv"}');
insert into table1 values('{"ak2":"av2","bk2":"bv2"}');
select * from table1;
+------------------------------+
| json_dict |
+------------------------------+
| {"ak": "av", "bk": "bv"} |
| {"ak2": "av2", "bk2": "bv2"} |
+------------------------------+
2 rows in set (0.00 sec)
select json_dict->"$.*" from table1;
+------------------+
| json_dict->"$.*" |
+------------------+
| ["av", "bv"] |
| ["av2", "bv2"] |
+------------------+
2 rows in set (0.00 sec)
https://dev.mysql.com/doc/refman/8.0/en/json-search-functions.html#operator_json-column-path
1
Thanks for the fiddle. also +1 to answer :)
– Madhur Bhaiya
Nov 28 '18 at 10:12
add a comment |
You can use the ->
operator as in the expression column -> path
as follows:
create table table1 (
json_dict JSON
);
insert into table1 values('{"ak":"av","bk":"bv"}');
insert into table1 values('{"ak2":"av2","bk2":"bv2"}');
select * from table1;
+------------------------------+
| json_dict |
+------------------------------+
| {"ak": "av", "bk": "bv"} |
| {"ak2": "av2", "bk2": "bv2"} |
+------------------------------+
2 rows in set (0.00 sec)
select json_dict->"$.*" from table1;
+------------------+
| json_dict->"$.*" |
+------------------+
| ["av", "bv"] |
| ["av2", "bv2"] |
+------------------+
2 rows in set (0.00 sec)
https://dev.mysql.com/doc/refman/8.0/en/json-search-functions.html#operator_json-column-path
1
Thanks for the fiddle. also +1 to answer :)
– Madhur Bhaiya
Nov 28 '18 at 10:12
add a comment |
You can use the ->
operator as in the expression column -> path
as follows:
create table table1 (
json_dict JSON
);
insert into table1 values('{"ak":"av","bk":"bv"}');
insert into table1 values('{"ak2":"av2","bk2":"bv2"}');
select * from table1;
+------------------------------+
| json_dict |
+------------------------------+
| {"ak": "av", "bk": "bv"} |
| {"ak2": "av2", "bk2": "bv2"} |
+------------------------------+
2 rows in set (0.00 sec)
select json_dict->"$.*" from table1;
+------------------+
| json_dict->"$.*" |
+------------------+
| ["av", "bv"] |
| ["av2", "bv2"] |
+------------------+
2 rows in set (0.00 sec)
https://dev.mysql.com/doc/refman/8.0/en/json-search-functions.html#operator_json-column-path
You can use the ->
operator as in the expression column -> path
as follows:
create table table1 (
json_dict JSON
);
insert into table1 values('{"ak":"av","bk":"bv"}');
insert into table1 values('{"ak2":"av2","bk2":"bv2"}');
select * from table1;
+------------------------------+
| json_dict |
+------------------------------+
| {"ak": "av", "bk": "bv"} |
| {"ak2": "av2", "bk2": "bv2"} |
+------------------------------+
2 rows in set (0.00 sec)
select json_dict->"$.*" from table1;
+------------------+
| json_dict->"$.*" |
+------------------+
| ["av", "bv"] |
| ["av2", "bv2"] |
+------------------+
2 rows in set (0.00 sec)
https://dev.mysql.com/doc/refman/8.0/en/json-search-functions.html#operator_json-column-path
answered Nov 28 '18 at 10:03
marekfulmarekful
10.1k42143
10.1k42143
1
Thanks for the fiddle. also +1 to answer :)
– Madhur Bhaiya
Nov 28 '18 at 10:12
add a comment |
1
Thanks for the fiddle. also +1 to answer :)
– Madhur Bhaiya
Nov 28 '18 at 10:12
1
1
Thanks for the fiddle. also +1 to answer :)
– Madhur Bhaiya
Nov 28 '18 at 10:12
Thanks for the fiddle. also +1 to answer :)
– Madhur Bhaiya
Nov 28 '18 at 10:12
add a comment |
Other than ->
operator, you can also use the Json_Extract()
function:
Schema (MySQL v8.0)
create table table1 (
json_dict JSON
);
insert into table1 values('{"ak":"av","bk":"bv"}');
insert into table1 values('{"ak2":"av2","bk2":"bv2"}');
Query #1
select JSON_EXTRACT(json_dict, '$.*') from table1;
| JSON_EXTRACT(json_dict, '$.*') |
| ------------------------------ |
| ["av", "bv"] |
| ["av2", "bv2"] |
View on DB Fiddle
I was faster and you copied code from my answer. However, I upvoted yours because you showed a slightly different alternative :)
– marekful
Nov 28 '18 at 10:10
@marekful Thanks for the fiddle. I was lazy to create a fiddle; so pouched on your fiddle :P
– Madhur Bhaiya
Nov 28 '18 at 10:11
add a comment |
Other than ->
operator, you can also use the Json_Extract()
function:
Schema (MySQL v8.0)
create table table1 (
json_dict JSON
);
insert into table1 values('{"ak":"av","bk":"bv"}');
insert into table1 values('{"ak2":"av2","bk2":"bv2"}');
Query #1
select JSON_EXTRACT(json_dict, '$.*') from table1;
| JSON_EXTRACT(json_dict, '$.*') |
| ------------------------------ |
| ["av", "bv"] |
| ["av2", "bv2"] |
View on DB Fiddle
I was faster and you copied code from my answer. However, I upvoted yours because you showed a slightly different alternative :)
– marekful
Nov 28 '18 at 10:10
@marekful Thanks for the fiddle. I was lazy to create a fiddle; so pouched on your fiddle :P
– Madhur Bhaiya
Nov 28 '18 at 10:11
add a comment |
Other than ->
operator, you can also use the Json_Extract()
function:
Schema (MySQL v8.0)
create table table1 (
json_dict JSON
);
insert into table1 values('{"ak":"av","bk":"bv"}');
insert into table1 values('{"ak2":"av2","bk2":"bv2"}');
Query #1
select JSON_EXTRACT(json_dict, '$.*') from table1;
| JSON_EXTRACT(json_dict, '$.*') |
| ------------------------------ |
| ["av", "bv"] |
| ["av2", "bv2"] |
View on DB Fiddle
Other than ->
operator, you can also use the Json_Extract()
function:
Schema (MySQL v8.0)
create table table1 (
json_dict JSON
);
insert into table1 values('{"ak":"av","bk":"bv"}');
insert into table1 values('{"ak2":"av2","bk2":"bv2"}');
Query #1
select JSON_EXTRACT(json_dict, '$.*') from table1;
| JSON_EXTRACT(json_dict, '$.*') |
| ------------------------------ |
| ["av", "bv"] |
| ["av2", "bv2"] |
View on DB Fiddle
answered Nov 28 '18 at 10:07
Madhur BhaiyaMadhur Bhaiya
19.6k62236
19.6k62236
I was faster and you copied code from my answer. However, I upvoted yours because you showed a slightly different alternative :)
– marekful
Nov 28 '18 at 10:10
@marekful Thanks for the fiddle. I was lazy to create a fiddle; so pouched on your fiddle :P
– Madhur Bhaiya
Nov 28 '18 at 10:11
add a comment |
I was faster and you copied code from my answer. However, I upvoted yours because you showed a slightly different alternative :)
– marekful
Nov 28 '18 at 10:10
@marekful Thanks for the fiddle. I was lazy to create a fiddle; so pouched on your fiddle :P
– Madhur Bhaiya
Nov 28 '18 at 10:11
I was faster and you copied code from my answer. However, I upvoted yours because you showed a slightly different alternative :)
– marekful
Nov 28 '18 at 10:10
I was faster and you copied code from my answer. However, I upvoted yours because you showed a slightly different alternative :)
– marekful
Nov 28 '18 at 10:10
@marekful Thanks for the fiddle. I was lazy to create a fiddle; so pouched on your fiddle :P
– Madhur Bhaiya
Nov 28 '18 at 10:11
@marekful Thanks for the fiddle. I was lazy to create a fiddle; so pouched on your fiddle :P
– Madhur Bhaiya
Nov 28 '18 at 10:11
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53516167%2fextract-json-objects-values-as-array-from-mysql-json-column%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Does mysqlserverteam.com/mysql-8-0-labs-json-aggregation-functions help?
– Nico Haase
Nov 28 '18 at 9:39
Can you post the SHOW CREATE TABLE result?
– marekful
Nov 28 '18 at 9:44
Are your key names all different or repeating?
– marekful
Nov 28 '18 at 9:54