I have some data I need to store inside a mysql table. The table has a few columns, among them a book and authors column, both of which are TEXT fields. I need to store a javascript object in the book column and an array of objects in the authors column.

Inside my angular component, I have the book object and authors array of objects.

const book = {
   title: "hello world",
   subtitle: "learn C programming in a week",
   cover: "path/to/cover.png"

const authors = [{id: 3, name: "John Doe"}, {id: 4, name: "Jane Doe"}];

The object I'm passing to my service to handle the post request looks something like the following.

   id: this.generateId(),
   category: this.category,
   book: JSON.stringify(book),
   authors: JSON.stringify(authors),
   price: this.price

Both of these are being stringified via JSON.stringify(...) and then successfully being posted to the database. No problem there. I should also mention that the object values are being grabbed and put into an SQL statement in the POST request like so...

`INSERT INTO library (id, category, book, authors)
 VALUES ('${obj['id']}',

However, when I try to retrieve data from that table via my angular service, angular throws the error Http failure during parsing for http://localhost:4200/.

When I fire the same request via postman, I get back the data but then when I check the validity of the returned JSON, it's not valid. The returned data looks something like this... The title" gets highlighted with the error Expecting comma, colon or }, not string

    "book": "{
      "title": "hello world",
      "subtitle": "learn C programming in a week",
      "cover": "path/to/cover.png"
    "authors": "[{"id": "3", "name": "John Doe"}, {"id": "4", "name": "Jane Doe"}]"

What am I missing? How do I get back valid JSON?

1 Answers

Joseph Bailey On

"book": "{ - this looks like you have stringyfied the two values separately and then added to one object?

If so create one object first and stringify the entire thing.