1

Postgres 12 with the following jsonb field I need to dig into:

{"count":894,
"limit":100000,
"units":"lin",
"observations": [
 {"date":"1947年01月01日","value":"21.48","realtime_end":"2021年07月14日","realtime_start":"2021年07月14日"},
 {"date":"1947年02月01日","value":"21.62","realtime_end":"2021年07月14日","realtime_start":"2021年07月14日"},
 {"date":"1947年03月01日","value":"22.0","realtime_end":"2021年07月14日","realtime_start":"2021年07月14日"}
 {"date":"1947年04月01日","value":"122.0","realtime_end":"2021年07月14日","realtime_start":"2021年07月14日"}
]}

I want to be able to get the entire observations record when doing a select. This gets me somewhat close:

SELECT jsonb_path_query(series_data_point, '$.observations.value[*] ? (@ >= "22.0").double()') as value
from table

but only gives me the value back, and it's string matching so it's not returning the last record there. Need to figure out how to match properly on nested values so I can filter based on values, dates, etc. and get a row back per matching observation.

Thank you! New to PostgreSQL and JSON Path syntax..

Charlieface
17.6k22 silver badges45 bronze badges
asked Jul 15, 2021 at 18:16

1 Answer 1

0

You need to move the ? filter onto the observations node, and within that, filter on value.

Also, I think you want 22.0 without quotes, because it is comparing to a double

SELECT jsonb_path_query(
 series_data_point,
 '$.observations ? (@.value[*].double() >= 22.0)') as value
from table
answered Jul 15, 2021 at 19:31
1
  • Thank you! I felt I was close and dancing around it but just didn't have the syntax quite right. Commented Jul 15, 2021 at 19:37

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.