0

I would like to be able to select the rows where "likes" is greater than 50.

 {
  "id":2,
  "name":"TEST",
  "locations":[
    {
      "cityName":"NEW YORK",
      "lat":123, "lon":456,
      "likes":1000,
      "dislikes":5
    }
  ]
}
2

2 Answers 2

0

How about this?

SELECT * FROM mytable WHERE JSON_EXTRACT_PATH_TEXT( jsoncolumn, 'likes' )::INTEGER > 50;
Sign up to request clarification or add additional context in comments.

Comments

0

Assuming data type json and that your array always has just the one element like demonstrated:

SELECT *
FROM   tbl
WHERE  (js_column #>> '{locations,0,likes}')::int > 50;

Using the json / jsonb #>> operator that takes a text array as path. Array index starts with 0 in a JSON array (as opposed to Postgres array counting from 1).

If the array can have multiple elements (which is the point of having a array in the first place) and you want any row where at least one element record qualifies, things get more sophisticated:

SELECT *
FROM   tbl t
WHERE  EXISTS (
   SELECT FROM json_array_elements(t.js_column->'locations') elem
   WHERE  (elem->>'likes')::int > 50
   );

Related, with explanation:

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.