Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get raw string representation #722

Closed
NotBobTheBuilder opened this issue Feb 5, 2015 · 4 comments
Closed

Get raw string representation #722

NotBobTheBuilder opened this issue Feb 5, 2015 · 4 comments

Comments

@NotBobTheBuilder
Copy link

I have a query which returns JSON, which I want to send back to a client in string form.

node-postgres parses my query result into an actual JavaScript object - which is useful in most cases - but here it seems wasteful to have to JSON.stringify what the library just JSON.parsed.

Is there a way to access the raw query result as a string (or perhaps change the query so it doesn’t get parsed at all), so I can just res.send the string?

Thanks!

@NotBobTheBuilder
Copy link
Author

(For now I’ve just put pg.types.setTypeParser(114, function(x) { return x; }); after the pg import - is there a better way?

@sehrope
Copy link
Contributor

sehrope commented Feb 11, 2015

If you cast the column to text in the query SQL then it'll be sent over the wire as is. Ex:

SELECT some_json::text FROM (SELECT '{"foo":"bar"}'::json AS some_json) t;

@jleppert
Copy link

+1. The big benefit here is to have all the JSON serialization already done for you, in this case the use case is proxying to a web client. What do people think of adding a config option to make this more explicit?

@brianc
Copy link
Owner

brianc commented Jun 21, 2016

If you want json to be represented as text, @sehrope has the right approach!

@brianc brianc closed this as completed Jun 21, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants