CREATE OR REPLACE FUNCTION LineStringFromPoints(linejsonb JSONB)
RETURNS geometry AS
$BODY$
DECLARE
p geometry;
i RECORD;
srid INTEGER = 4326;
points GEOMETRY [];
str TEXT;
BEGIN
FOR i IN SELECT
cast(t ->> 'lng' AS FLOAT) AS lng,
cast(t ->> 'lat' AS FLOAT) AS lat
FROM jsonb_array_elements(linejsonb) t
LOOP
points = points || st_makepoint(i.lng, i.lat);
END LOOP;
RETURN st_makeline(points);
END;
$BODY$
LANGUAGE PLPGSQL;