Search code examples
jsonmariadbjson-query

Fetching object JSON array using JSON_QUERY in MariaDB


How do I access the value of a object property, inside an array, using JSON_QUERY in MariaDB? I have a JSON field with following data, and want to access the value of the section property.

[
  [
    {"section": "search"}
  ]
]

The following SQL is returning NULL

SELECT JSON_QUERY('[[{"section": "search"}]]', '$[0][0].section') FROM mytable

Solution

  • For anyone else having the same issue, you have to use JSON_VALUE for accessing scalar values. The following query gives the expected result.

    SELECT JSON_VALUE'[[{"section": "search"}]]', '$[0][0].section') FROM mytable;
    

    JSON_QUERY vs JSON_VALUE