So, I realize this question gets asked a lot, but the root of my question is related to the syntax of the SQL statement used to format the result table as GeoJSON. I am currently able to execute a SELECT *
statement using this code which I adapted from an example I found online
SELECT row_to_json(fc)
FROM ( SELECT 'FeatureCollection' As type, array_to_json(array_agg(f)) As features
FROM (SELECT 'Feature' As type
, ST_AsGeoJSON(lg.geom)::json As geometry
, row_to_json((SELECT l FROM (SELECT id, filename, type, \"desc\") As l
)) As properties
FROM public.\"Peng\" As lg ) As f ) As fc;
The majority of examples suggest using the json_build_object
function, but the examples I've found are a bit opaque to me, particularly the nested statements and truncated variables. Conceptually, I know the statements are creating the features collection and then the nested features, but it's unclear how to reference my tables and fields correctly. In my case I'm trying to get the results of this query returned as JSON for my app: SELECT * FROM public."Peng", public."Address_Info" WHERE "Peng".id = "Address_Info".id;
Eventually, I would be interested pulling specific fields, but for now I'd just be happy to understand how to adapt either my, or a json_build_object
example to my use case. Additionally, any insight into how this syntax works, and the shortened variables and such.
ST_AsGeoJSON(RECORD)
function into a moving aggregate UDF here: it returns a FeatureCollection asJSONB
. You'd create a table with the desired properties and the geometry in a sub-query, as per the docs. Might be a handy function set if you need to create these regularly.