2

I have the following JSON-File in a MYSQL Database:

[
{
    "order" : 0,
    "value" : 0.168257
},
{
    "order" : 0.031250,
    "value" : 0.002387
},
{
    "order" : 0.062500,
    "value" : 0.002367
},
{
    "order" : 0.093750,
    "value" : 0.002365
},
{
    "order" : 0.125000,
    "value" : 0.002369
},
{
    "order" : 0.156250,
    "value" : 0.002384
   },
   {
    "order" : 0.187500,
    "value" : 0.002403
   }
]

I would like to Query and get the result for "order"=0.156250 I use the following Query:

JSON_EXTRACT(jsonColumn,'$.order') ... WHERE JSON_EXTRACT(jsonColumn,'$.order') = 0.156250

It is not working. I can only select the Column by doing the following:

Select JSON_EXTRACT(jsonColumn,'$[5].order')

Can somebody tell me how to select the Column without giving the index to the Select statement?

Thanks!

4
  • Which version of MySQL? Commented Nov 26, 2019 at 8:11
  • Also, are the values numeric or strings? Commented Nov 26, 2019 at 8:12
  • MySQL version 5.7 or 8.0? Commented Nov 26, 2019 at 8:13
  • MySQL Version 8.0 Commented Nov 26, 2019 at 8:34

1 Answer 1

2

You need to convert this to json_table first before filtering

select *
     from
test t1
cross join 
       json_table(t1.jsonColumn,
         "$[*]"
         columns(
           `Order` numeric(9,6) path "$.order",        
           `Value` numeric(9,6) path "$.value"
         )
       ) t2
where t2.Order = 0.156250

see dbfiddle

for mySQL versions 5.7

select
  *  
from test t1  
cross join ( 
  select  1 as col1 union
  select  2 union
  select  3 union
  select  4 union
  select  5 union
  select  6   
  ) t2
where json_extract(t1.jsonColumn, concat('$[', t2.col1, '].order')) = 0.156250;

see dbfiddle

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.