Extract JSON object's values as array from MySQL JSON column












4















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









share|improve this question

























  • 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
















4















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









share|improve this question

























  • 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














4












4








4








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









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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



















  • 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












2 Answers
2






active

oldest

votes


















2














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






share|improve this answer



















  • 1





    Thanks for the fiddle. also +1 to answer :)

    – Madhur Bhaiya
    Nov 28 '18 at 10:12



















1














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






share|improve this answer
























  • 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











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
});


}
});














draft saved

draft discarded


















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









2














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






share|improve this answer



















  • 1





    Thanks for the fiddle. also +1 to answer :)

    – Madhur Bhaiya
    Nov 28 '18 at 10:12
















2














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






share|improve this answer



















  • 1





    Thanks for the fiddle. also +1 to answer :)

    – Madhur Bhaiya
    Nov 28 '18 at 10:12














2












2








2







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






share|improve this answer













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







share|improve this answer












share|improve this answer



share|improve this answer










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














  • 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













1














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






share|improve this answer
























  • 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
















1














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






share|improve this answer
























  • 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














1












1








1







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






share|improve this answer













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







share|improve this answer












share|improve this answer



share|improve this answer










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



















  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks