How to get a json object with specific key values, from a json array column?

Jordan Youngs

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.

Nick

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

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to parse json to get all values of a specific key within an array?

分類Dev

How to get values from Deserialized JSON object?

分類Dev

fetch json object from json array with a key

分類Dev

How can I get the key name and its value from an array within a JSON object

分類Dev

How can I get a specific value from my JSON object without using Array.filter()?

分類Dev

How to get a name value from a foreign key of a Json object

分類Dev

Return specific values from array of hashes - JSON

分類Dev

angularjs how to post key/values not json object

分類Dev

get data from JSON Object according to a key

分類Dev

how to get a particular object value from nested json array

分類Dev

Want to get a specific Element from a json array,

分類Dev

remove all values from JSON matching specific key

分類Dev

How to find values from another JSON object

分類Dev

$.ajax get an object after json_encode($arr) from php, but how to get key and value in jQuery?

分類Dev

How to get the count of repeated key values of array object of consecutive elements?

分類Dev

How to access JSON-object with multiple values per key?

分類Dev

How to get Values of one JSON object into the Values of another with Python

分類Dev

How to get all Object from Json to List

分類Dev

How to get unique values from specific column in SQL Server?

分類Dev

How to query JSON column for unique object values in PostgreSQL

分類Dev

PHP - Get all values with specific array key from multidimensional array with unknown depth

分類Dev

How to get json value from json array using javascript

分類Dev

jQuery - How to get JSON array name from nested JSON

分類Dev

How to get JSON object by one of its values in Java?

分類Dev

How to get the values of a column from a table in jQuery into a multidimentional array

分類Dev

Get column name from JSON

分類Dev

Create json with column values as object keys

分類Dev

How to get JSON response array all index values?

分類Dev

How to object values in specified manner from json having different nesting

Related 関連記事

  1. 1

    How to parse json to get all values of a specific key within an array?

  2. 2

    How to get values from Deserialized JSON object?

  3. 3

    fetch json object from json array with a key

  4. 4

    How can I get the key name and its value from an array within a JSON object

  5. 5

    How can I get a specific value from my JSON object without using Array.filter()?

  6. 6

    How to get a name value from a foreign key of a Json object

  7. 7

    Return specific values from array of hashes - JSON

  8. 8

    angularjs how to post key/values not json object

  9. 9

    get data from JSON Object according to a key

  10. 10

    how to get a particular object value from nested json array

  11. 11

    Want to get a specific Element from a json array,

  12. 12

    remove all values from JSON matching specific key

  13. 13

    How to find values from another JSON object

  14. 14

    $.ajax get an object after json_encode($arr) from php, but how to get key and value in jQuery?

  15. 15

    How to get the count of repeated key values of array object of consecutive elements?

  16. 16

    How to access JSON-object with multiple values per key?

  17. 17

    How to get Values of one JSON object into the Values of another with Python

  18. 18

    How to get all Object from Json to List

  19. 19

    How to get unique values from specific column in SQL Server?

  20. 20

    How to query JSON column for unique object values in PostgreSQL

  21. 21

    PHP - Get all values with specific array key from multidimensional array with unknown depth

  22. 22

    How to get json value from json array using javascript

  23. 23

    jQuery - How to get JSON array name from nested JSON

  24. 24

    How to get JSON object by one of its values in Java?

  25. 25

    How to get the values of a column from a table in jQuery into a multidimentional array

  26. 26

    Get column name from JSON

  27. 27

    Create json with column values as object keys

  28. 28

    How to get JSON response array all index values?

  29. 29

    How to object values in specified manner from json having different nesting

ホットタグ

アーカイブ