1

I want to get the JSON response from the function response and I'm able to achieve that. But the problem is on every response I'm getting the function name also how to fix this issue?

Current response

[
    {
        "__find_attractions_return_json": {
            "name": "Conrad Pond",
            "lat": 41.01696,
            "lon": -74.22462
        }
    },
    {
        "__find_attractions_return_json": {
            "name": "Crossway Creek",
            "lat": 40.46122,
            "lon": -74.26292
        }
    },
    .....
    .....
]

Required result

[
    {
            "name": "Conrad Pond",
            "lat": 41.01696,
            "lon": -74.22462
    },
    {
            "name": "Crossway Creek",
            "lat": 40.46122,
            "lon": -74.26292
    },
    .....
    .....
]

POSTGRES function

CREATE OR REPLACE FUNCTION public.__find_attractions_return_json(point character varying, cty varchar)
    RETURNS SETOF json 
    LANGUAGE 'plpgsql'

    COST 100
    VOLATILE 
    ROWS 1000
AS $BODY$
BEGIN
  RETURN QUERY
SELECT 
    json_build_object( 
        'name',  name, 
        'lat', lat, 
        'lon', lon
        )
    FROM places
    WHERE country=cty 
    AND ST_DWithin(geom::geography,ST_GeogFromText(point), 50000, false);
END
$BODY$;

I'm calling this function from NodeJs.

Any kind of help will much be appreciated.

1 Answer 1

1

demo:db<>fiddle

SELECT
    json_agg(elements -> '__find_attractions_return_json')
FROM 
    mytable,
    json_array_elements(mydata) as elements
  1. json_array_elements expands all array elements into one row each
  2. for every row getting the value of your removeable key
  3. json_agg aggregates these value objects
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.