bendrucker / ama

Ask me questions about building web applications
MIT License
6 stars 1 forks source link

AngularJS 1.4 and PostgreSQL 9.4 JSON Arrays #9

Closed demisx closed 9 years ago

demisx commented 9 years ago

Hi Ben, Wondering what your thought is about this. I have a jsonb[]type photos column in my table created as:

table.specificType('photos', 'jsonb[]');

This column will store array of JSON objects. Currently, when I seed it with this data:

photos: [
  { url: "https://example.com/photo1.jpg"} ,
  { url: "https://example.com/photo2.jpg"}
]

PostgreSQL 9.4 stores it as:

{"{\"url\": \"https://example.com/photo1.jpg\"}","{\"url\": \"https://example.com/photo2.jpg\"}"}

Can you please recommend the best approach to convert this Postgres' representation of JSON array to the conventional [{...}, {...}, ...] kind and the layer where it should be done at (e.g. Postgres, Bookshelf, Angular)? The API returns this filed to the AngularJS frontend and I want Angular to be able to see the conventional kind, not the Postgres version of it. I've tried to find similar scenarios online, but couldn't so far.

Thank you.

bendrucker commented 9 years ago

Is it not parsed back to an array by node-postgres?

demisx commented 9 years ago

I am not using node-postgres at all. Do I need to?

demisx commented 9 years ago

Sorry, didn't realize that pg was the node-postgres. Yeah, I am using it, but API still responds with the Postgres version of the JSON array. Is this something that needs to be converted by the pg?

bendrucker commented 9 years ago

What does that mean? Because it parses json fine. But yeah response parsing is handled by the db driver.

demisx commented 9 years ago

OK. I will file an issue with node-postgres then. Thank you.

demisx commented 9 years ago

FYI, looks like there is no JSON array parsing in node-postgres yet. See https://github.com/brianc/node-postgres/pull/456#issuecomment-75909527. Please fill free to chime in, if you can recommend a good parsing library that Brian is asking about.