How to convert an object to JSON correctly in Angular 2 with TypeScript How to convert an object to JSON correctly in Angular 2 with TypeScript angular angular

How to convert an object to JSON correctly in Angular 2 with TypeScript


In your product.service.ts you are using stringify method in a wrong way..

Just use

JSON.stringify(product) 

instead of

JSON.stringify({product})

i have checked your problem and after this it's working absolutely fine.


You'll have to parse again if you want it in actual JSON:

JSON.parse(JSON.stringify(object))


If you are solely interested in outputting the JSON somewhere in your HTML, you could also use a pipe inside an interpolation. For example:

<p> {{ product | json }} </p>

I am not entirely sure it works for every AngularJS version, but it works perfectly in my Ionic App (which uses Angular 2+).