How to get a json object with specific key values, from a json array column?
In my MySQL 8.0 table, I have a JSON ARRAY column. It is an array of JSON objects. I want to pick one object out of each row's array, based on the key value pairs in the objects.
Example row:
[{bool:false, number:0, value:'hello'},
{bool:true, number:1, value:'world'},
{bool:true, number:2, value:'foo'},
{bool:false, number:1, value:'bar'}]
What I am trying to do is get the 'value' WHERE bool=true, AND number=1. So I want a query that in this example returns 'world'.
What would also work is if I could get the index of the object where bool=true and number=1, in this example it would return '$[1]'.
I am trying to run a query across the whole column, setting a new column to the value returned from the query. Is this possible with MySQL JSON functions? I've looked at the references but none have objects inside arrays like my example.
EDIT: If I do
SELECT JSON_SEARCH(column->"$[*]", 'all', '1');
SELECT JSON_SEARCH(names->"$[*]", 'all', 'true');
I get the paths/indexes of objects where number=1, and where bool=true, respectively. I would like the overlap of these two results.
mysql arrays json object
add a comment |
In my MySQL 8.0 table, I have a JSON ARRAY column. It is an array of JSON objects. I want to pick one object out of each row's array, based on the key value pairs in the objects.
Example row:
[{bool:false, number:0, value:'hello'},
{bool:true, number:1, value:'world'},
{bool:true, number:2, value:'foo'},
{bool:false, number:1, value:'bar'}]
What I am trying to do is get the 'value' WHERE bool=true, AND number=1. So I want a query that in this example returns 'world'.
What would also work is if I could get the index of the object where bool=true and number=1, in this example it would return '$[1]'.
I am trying to run a query across the whole column, setting a new column to the value returned from the query. Is this possible with MySQL JSON functions? I've looked at the references but none have objects inside arrays like my example.
EDIT: If I do
SELECT JSON_SEARCH(column->"$[*]", 'all', '1');
SELECT JSON_SEARCH(names->"$[*]", 'all', 'true');
I get the paths/indexes of objects where number=1, and where bool=true, respectively. I would like the overlap of these two results.
mysql arrays json object
add a comment |
In my MySQL 8.0 table, I have a JSON ARRAY column. It is an array of JSON objects. I want to pick one object out of each row's array, based on the key value pairs in the objects.
Example row:
[{bool:false, number:0, value:'hello'},
{bool:true, number:1, value:'world'},
{bool:true, number:2, value:'foo'},
{bool:false, number:1, value:'bar'}]
What I am trying to do is get the 'value' WHERE bool=true, AND number=1. So I want a query that in this example returns 'world'.
What would also work is if I could get the index of the object where bool=true and number=1, in this example it would return '$[1]'.
I am trying to run a query across the whole column, setting a new column to the value returned from the query. Is this possible with MySQL JSON functions? I've looked at the references but none have objects inside arrays like my example.
EDIT: If I do
SELECT JSON_SEARCH(column->"$[*]", 'all', '1');
SELECT JSON_SEARCH(names->"$[*]", 'all', 'true');
I get the paths/indexes of objects where number=1, and where bool=true, respectively. I would like the overlap of these two results.
mysql arrays json object
In my MySQL 8.0 table, I have a JSON ARRAY column. It is an array of JSON objects. I want to pick one object out of each row's array, based on the key value pairs in the objects.
Example row:
[{bool:false, number:0, value:'hello'},
{bool:true, number:1, value:'world'},
{bool:true, number:2, value:'foo'},
{bool:false, number:1, value:'bar'}]
What I am trying to do is get the 'value' WHERE bool=true, AND number=1. So I want a query that in this example returns 'world'.
What would also work is if I could get the index of the object where bool=true and number=1, in this example it would return '$[1]'.
I am trying to run a query across the whole column, setting a new column to the value returned from the query. Is this possible with MySQL JSON functions? I've looked at the references but none have objects inside arrays like my example.
EDIT: If I do
SELECT JSON_SEARCH(column->"$[*]", 'all', '1');
SELECT JSON_SEARCH(names->"$[*]", 'all', 'true');
I get the paths/indexes of objects where number=1, and where bool=true, respectively. I would like the overlap of these two results.
mysql arrays json object
mysql arrays json object
edited Jan 18 at 19:49
Jordan Youngs
asked Jan 18 at 19:38
Jordan YoungsJordan Youngs
83
83
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You can use JSON_TABLE
to convert the JSON into a derived table which you can then extract values from:
SELECT j.value
FROM test t
JOIN JSON_TABLE(t.jsonStr,
'$[*]'
COLUMNS(bool BOOLEAN PATH '$.bool',
number INT PATH '$.number',
value VARCHAR(20) PATH '$.value')) j
WHERE j.bool = true AND j.number = 1
Output:
value
world
If you also want to get the index within each JSON value of the value
which matched, you can add a FOR ORDINALITY
clause to your JSON_TABLE
e.g.:
SELECT j.idx, j.value
FROM test t
JOIN JSON_TABLE(t.jsonStr,
'$[*]'
COLUMNS(idx FOR ORDINALITY,
bool BOOLEAN PATH '$.bool',
number INT PATH '$.number',
value VARCHAR(20) PATH '$.value')) j
WHERE j.bool = true AND j.number = 1
Output:
idx value
2 world
Demo on dbfiddle
Thank you! This worked and your examples were very helpful
– Jordan Youngs
Jan 21 at 16:00
@JordanYoungs no worries. Glad I could help.
– Nick
Jan 21 at 21:18
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%2f54260416%2fhow-to-get-a-json-object-with-specific-key-values-from-a-json-array-column%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use JSON_TABLE
to convert the JSON into a derived table which you can then extract values from:
SELECT j.value
FROM test t
JOIN JSON_TABLE(t.jsonStr,
'$[*]'
COLUMNS(bool BOOLEAN PATH '$.bool',
number INT PATH '$.number',
value VARCHAR(20) PATH '$.value')) j
WHERE j.bool = true AND j.number = 1
Output:
value
world
If you also want to get the index within each JSON value of the value
which matched, you can add a FOR ORDINALITY
clause to your JSON_TABLE
e.g.:
SELECT j.idx, j.value
FROM test t
JOIN JSON_TABLE(t.jsonStr,
'$[*]'
COLUMNS(idx FOR ORDINALITY,
bool BOOLEAN PATH '$.bool',
number INT PATH '$.number',
value VARCHAR(20) PATH '$.value')) j
WHERE j.bool = true AND j.number = 1
Output:
idx value
2 world
Demo on dbfiddle
Thank you! This worked and your examples were very helpful
– Jordan Youngs
Jan 21 at 16:00
@JordanYoungs no worries. Glad I could help.
– Nick
Jan 21 at 21:18
add a comment |
You can use JSON_TABLE
to convert the JSON into a derived table which you can then extract values from:
SELECT j.value
FROM test t
JOIN JSON_TABLE(t.jsonStr,
'$[*]'
COLUMNS(bool BOOLEAN PATH '$.bool',
number INT PATH '$.number',
value VARCHAR(20) PATH '$.value')) j
WHERE j.bool = true AND j.number = 1
Output:
value
world
If you also want to get the index within each JSON value of the value
which matched, you can add a FOR ORDINALITY
clause to your JSON_TABLE
e.g.:
SELECT j.idx, j.value
FROM test t
JOIN JSON_TABLE(t.jsonStr,
'$[*]'
COLUMNS(idx FOR ORDINALITY,
bool BOOLEAN PATH '$.bool',
number INT PATH '$.number',
value VARCHAR(20) PATH '$.value')) j
WHERE j.bool = true AND j.number = 1
Output:
idx value
2 world
Demo on dbfiddle
Thank you! This worked and your examples were very helpful
– Jordan Youngs
Jan 21 at 16:00
@JordanYoungs no worries. Glad I could help.
– Nick
Jan 21 at 21:18
add a comment |
You can use JSON_TABLE
to convert the JSON into a derived table which you can then extract values from:
SELECT j.value
FROM test t
JOIN JSON_TABLE(t.jsonStr,
'$[*]'
COLUMNS(bool BOOLEAN PATH '$.bool',
number INT PATH '$.number',
value VARCHAR(20) PATH '$.value')) j
WHERE j.bool = true AND j.number = 1
Output:
value
world
If you also want to get the index within each JSON value of the value
which matched, you can add a FOR ORDINALITY
clause to your JSON_TABLE
e.g.:
SELECT j.idx, j.value
FROM test t
JOIN JSON_TABLE(t.jsonStr,
'$[*]'
COLUMNS(idx FOR ORDINALITY,
bool BOOLEAN PATH '$.bool',
number INT PATH '$.number',
value VARCHAR(20) PATH '$.value')) j
WHERE j.bool = true AND j.number = 1
Output:
idx value
2 world
Demo on dbfiddle
You can use JSON_TABLE
to convert the JSON into a derived table which you can then extract values from:
SELECT j.value
FROM test t
JOIN JSON_TABLE(t.jsonStr,
'$[*]'
COLUMNS(bool BOOLEAN PATH '$.bool',
number INT PATH '$.number',
value VARCHAR(20) PATH '$.value')) j
WHERE j.bool = true AND j.number = 1
Output:
value
world
If you also want to get the index within each JSON value of the value
which matched, you can add a FOR ORDINALITY
clause to your JSON_TABLE
e.g.:
SELECT j.idx, j.value
FROM test t
JOIN JSON_TABLE(t.jsonStr,
'$[*]'
COLUMNS(idx FOR ORDINALITY,
bool BOOLEAN PATH '$.bool',
number INT PATH '$.number',
value VARCHAR(20) PATH '$.value')) j
WHERE j.bool = true AND j.number = 1
Output:
idx value
2 world
Demo on dbfiddle
edited Jan 18 at 23:23
answered Jan 18 at 20:20
NickNick
27.1k111940
27.1k111940
Thank you! This worked and your examples were very helpful
– Jordan Youngs
Jan 21 at 16:00
@JordanYoungs no worries. Glad I could help.
– Nick
Jan 21 at 21:18
add a comment |
Thank you! This worked and your examples were very helpful
– Jordan Youngs
Jan 21 at 16:00
@JordanYoungs no worries. Glad I could help.
– Nick
Jan 21 at 21:18
Thank you! This worked and your examples were very helpful
– Jordan Youngs
Jan 21 at 16:00
Thank you! This worked and your examples were very helpful
– Jordan Youngs
Jan 21 at 16:00
@JordanYoungs no worries. Glad I could help.
– Nick
Jan 21 at 21:18
@JordanYoungs no worries. Glad I could help.
– Nick
Jan 21 at 21:18
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%2f54260416%2fhow-to-get-a-json-object-with-specific-key-values-from-a-json-array-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