0

Would appreciate if you can help a mariaDB newbie with this.

I have a mariaDB database table called 'brand' with data as below.

Datatypes are VARCHAR and JSON.

name varchar(255) NOT NULL
sold longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid('sold'))
name sold
Test [{"id":"0","name":"0","count":5000},{"id":"1","name":"1","count":5000}]
Test2 [{"id":"0","name":"0","count":10000},{"id":"1","name":"1","count":10000}]
Test3 [{"id":"0","name":"0","count":15000},{"id":"1","name":"1","count":35000}]
Test4 [{"id":"0","name":"0","count":700},{"id":"1","name":"1","count":600}]

When I execute the query:

SELECT name, JSON_EXTRACT(sold, '$[*].count') as cnt FROM brands;
SELECT name, JSON_EXTRACT(sold, '$**.count') as cnt FROM brands;

I get the below output

name cnt
Test [5000, 5000]
Test2 10000
Test3 15000
Test4 700

I was expecting output as below

name cnt
Test [5000, 5000]
Test2 [10000, 10000]
Test3 [15000, 35000]
Test4 [700, 600]

Can you please help me correct the error?

  1. I have searched the mariaDB documentation https://mariadb.com/kb/en/json_extract/ but not able to find a solution yet.
  2. I also looked at this post: How do I extract values from a JSON array in MariaDB or MySQL? and I am implementing same SQL...

Thank you!

Update: 25 Jan

Ticket submitted to JIRA mariadb and fixed here

Baton8546
  • 1
  • 1
  • I see no errors with your setup. Please see [this test fiddle](https://dbfiddle.uk/kSQw2Xaz). Are these your exact queries? What's your MariaDB version? Also, be sure to check if there are no misspellings for the `count` keys for the data you're not getting. – FiddlingAway Jan 14 '23 at 13:12
  • Version I am using is 10.10.2-MariaDB. Thank you for the link. When I use version 10.9 in the fiddle link, it gives me the same 'wrong' output. When I use version 10.8 and lower, it gives me the expected output. Any idea why? – Baton8546 Jan 14 '23 at 13:28
  • Check [existing bug report](https://jira.mariadb.org/issues/?jql=text%20~%20%22json_extract%22%20and%20(status!%3DClosed%20or%20fixVersion%20%3D10.11.2)) and if not there, create a new one please. – danblack Jan 15 '23 at 06:08
  • I have submitted a new bug. – Baton8546 Jan 25 '23 at 10:01

0 Answers0