I've set up the following DB Fiddle: https://dbfiddle.uk/mzsp-4tg
How should I make a correct SQL query to use like
on geo_json
field of json
type to get table records with string matching coordinates combination %30.504106925781265,50.43731985018662%
?
-
Have you considered not storing coordinates as JSON?mustaccio– mustaccio2023年04月03日 18:33:27 +00:00Commented Apr 3, 2023 at 18:33
1 Answer 1
If you store or convert the column to jsonb
then you can use JSONpath
SELECT *
FROM polygon_versions
WHERE jsonb_path_exists(
geo_json,
'$.geoJSON.geometry.coordinates[*] ? (@[0] == 30.504106925781265 && @[1] == 50.43731985018662)');
answered Apr 3, 2023 at 18:38
-
How can I fix the following ERROR: function jsonb_path_exists(json, unknown) does not exist LINE 3: WHERE jsonb_path_exists( ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts. SQL state: 42883 Character: 38Taras– Taras2023年04月11日 14:11:42 +00:00Commented Apr 11, 2023 at 14:11
-
I have the following PostgreSQL version: PostgreSQL 14.7 (Debian 14.7-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bitTaras– Taras2023年04月11日 14:22:38 +00:00Commented Apr 11, 2023 at 14:22
-
Is the column
json
orjsonb
? This only works withjsonb
so either change the column's data type, or convert it like thisWHERE jsonb_path_exists(geo_json::jsonb, ...
Charlieface– Charlieface2023年04月14日 00:20:10 +00:00Commented Apr 14, 2023 at 0:20
lang-sql