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.

Answer from Akshay Rao on Stack Overflow
Top answer
1 of 12
75

If you use a TypeScript interface instead of a class, things are simpler:

Copyexport interface Employee {
    typeOfEmployee_id: number;
    department_id: number;
    permissions_id: number;
    maxWorkHours: number;
    employee_id: number;
    firstname: string;
    lastname: string;
    username: string;
    birthdate: Date;
    lastUpdate: Date;
}

let jsonObj = JSON.parse(employeeString); // string to "any" object first
let employee = jsonObj as Employee;

If you want a class, however, simple casting won't work. For example:

Copyclass Foo {
    name: string;
    public pump() { }
}

let jsonObj = JSON.parse('{ "name":"hello" }');
let fObj = jsonObj as Foo;
fObj.pump(); // crash, method is undefined!

For a class, you'll have to write a constructor which accepts a JSON string/object and then iterate through the properties to assign each member manually, like this:

Copyclass Foo {
    name: string;

    constructor(jsonStr: string) {
        let jsonObj = JSON.parse(jsonStr);
        for (let prop in jsonObj) {
            this[prop] = jsonObj[prop];
        }
    }
}

let fObj = new Foo(theJsonString);
2 of 12
45

The reason that the compiler lets you cast the object returned from JSON.parse to a class is because typescript is based on structural subtyping.
You don't really have an instance of an Employee, you have an object (as you see in the console) which has the same properties.

A simpler example:

Copyclass A {
    constructor(public str: string, public num: number) {}
}

function logA(a: A) {
    console.log(`A instance with str: "${ a.str }" and num: ${ a.num }`);
}

let a1 = { str: "string", num: 0, boo: true };
let a2 = new A("stirng", 0);
logA(a1); // no errors
logA(a2);

(code in playground)

There's no error because a1 satisfies type A because it has all of its properties, and the logA function can be called with no runtime errors even if what it receives isn't an instance of A as long as it has the same properties.

That works great when your classes are simple data objects and have no methods, but once you introduce methods then things tend to break:

Copyclass A {
    constructor(public str: string, public num: number) { }

    multiplyBy(x: number): number {
        return this.num * x;
    }
}

// this won't compile:
let a1 = { str: "string", num: 0, boo: true } as A; // Error: Type '{ str: string; num: number; boo: boolean; }' cannot be converted to type 'A'

// but this will:
let a2 = { str: "string", num: 0 } as A;

// and then you get a runtime error:
a2.multiplyBy(4); // Error: Uncaught TypeError: a2.multiplyBy is not a function

(code in playground)


Edit

This works just fine:

Copyconst employeeString = '{"department":"<anystring>","typeOfEmployee":"<anystring>","firstname":"<anystring>","lastname":"<anystring>","birthdate":"<anydate>","maxWorkHours":0,"username":"<anystring>","permissions":"<anystring>","lastUpdate":"<anydate>"}';
let employee1 = JSON.parse(employeeString);
console.log(employee1);

(code in playground)

If you're trying to use JSON.parse on your object when it's not a string:

Copylet e = {
    "department": "<anystring>",
    "typeOfEmployee": "<anystring>",
    "firstname": "<anystring>",
    "lastname": "<anystring>",
    "birthdate": "<anydate>",
    "maxWorkHours": 3,
    "username": "<anystring>",
    "permissions": "<anystring>",
    "lastUpdate": "<anydate>"
}
let employee2 = JSON.parse(e);

Then you'll get the error because it's not a string, it's an object, and if you already have it in this form then there's no need to use JSON.parse.

But, as I wrote, if you're going with this way then you won't have an instance of the class, just an object that has the same properties as the class members.

If you want an instance then:

Copylet e = new Employee();
Object.assign(e, {
    "department": "<anystring>",
    "typeOfEmployee": "<anystring>",
    "firstname": "<anystring>",
    "lastname": "<anystring>",
    "birthdate": "<anydate>",
    "maxWorkHours": 3,
    "username": "<anystring>",
    "permissions": "<anystring>",
    "lastUpdate": "<anydate>"
});
Discussions

Convert JSON to object type in Angular - Stack Overflow
I'm having real trouble converting a JSON array to collection of objects in Angular. I've not used Angular for some time, please forgive me if any of my terminology is incorrect. I have the follow... More on stackoverflow.com
🌐 stackoverflow.com
Convert JSON objects to JSOn array
Hi I have below data [object][object],[object][object],[object][object],[object][object]… etc So I want this all data in array format like [[object][object],[object][object],[object][object],[object][object]… etc] I used push() but it is not working. please help me. More on forum.ionicframework.com
🌐 forum.ionicframework.com
0
0
January 2, 2020
How to convert string to object in Angularjs [duplicate]
I know JSON.Parse() works, but there is something else to do it with angular, for some reasons. More on stackoverflow.com
🌐 stackoverflow.com
July 29, 2016
Angularjs converting json data to "[object Object]"
i am using Angular resource for ajax calls. I have a update transaction in which i am getting data from the database, while getting data the data is like this: Now after adding data to empty fields of this object i am again sending it to the server, before sending to the server the data is getting converted into different form like this: What can be the possible reason? I have already used JSON... More on stackoverflow.com
🌐 stackoverflow.com
October 28, 2016
🌐
Quora
quora.com › How-can-I-convert-a-JSON-format-string-into-a-real-object-in-JS
How to convert a JSON format string into a real object in JS - Quora
Answer (1 of 14): You could just use AngularJS: var jsObj = angular.fromJSON( ); Reference: AngularJS or use proper browser json parse: var jsObj = JSON.parse( ); Reference: JSON Howto
🌐
Angular
angular.dev › api › common › JsonPipe
JsonPipe • Angular
@Component({ selector: 'json-pipe', imports: [JsonPipe], template: `<div> <p>Without JSON pipe:</p> <pre>{{ object }}</pre> <p>With JSON pipe:</p> <pre>{{ object | json }}</pre> </div>`, }) export class JsonPipeComponent { object: Object = {foo: 'bar', baz: 'qux', nested: {xyz: 3, numbers: [1, 2, 3, 4, 5]}}; }
🌐
Cloudhadoop
cloudhadoop.com › home
How to convert json to/from an object in angular|Typescript
December 31, 2023 - This post explores different methods to convert JSON to an object or an object to JSON in Angular.
Find elsewhere
🌐
Ionic Framework
forum.ionicframework.com › ionic framework › ionic angular
Convert JSON objects to JSOn array - Ionic Angular - Ionic Forum
January 2, 2020 - Hi I have below data [object][object],[object][object],[object][object],[object][object]… etc So I want this all data in array format like [[object][object],[object][object],[object][object],[object][object]… etc] I …
🌐
Angular
v17.angular.io › api › @angular/common › jsonpipe
Angular - JsonPipe
content_copy @Component({ selector: 'json-pipe', template: `<div> <p>Without JSON pipe:</p> <pre>{{ object }}</pre> <p>With JSON pipe:</p> <pre>{{ object | json }}</pre> </div>`, }) export class JsonPipeComponent { object: Object = {foo: 'bar', baz: 'qux', nested: {xyz: 3, numbers: [1, 2, 3, 4, 5]}}; }
🌐
AngularJS
docs.angularjs.org › api › ng › filter › json
API: json
AngularJS is what HTML would have been, had it been designed for building web-apps. Declarative templates with data-binding, MVC, dependency injection and great testability story all implemented with pure client-side JavaScript!
🌐
C# Corner
c-sharpcorner.com › blogs › how-to-parse-and-stringify-json-data-using-angular
How To Parse And Stringify JSON Data Using Angular
October 5, 2018 - For that, we can use a method JSON.stringify(), which is used to convert the object data into the JSON format. Example · import { Component , OnInit } from '@angular/core'; @Component({ selector: 'my-app', templateUrl: './app.component.html', ...
🌐
GeeksforGeeks
geeksforgeeks.org › angularjs › angularjs-angular-tojson-function
AngularJS angular.toJson() Function - GeeksforGeeks
July 11, 2025 - The angular.toJson() Function in AngularJS is used to serialize the javascript object into a JSON - formatted string. It takes the javascript object and returns a JSON string. The angular.toJson() function in AngularJS is capable of handling ...
🌐
Stack Overflow
stackoverflow.com › questions › 40298851 › angularjs-converting-json-data-to-object-object
Angularjs converting json data to "[object Object]"
October 28, 2016 - I got the point why the data is converted, actually before sending data to server i am applying validation on data but the validation file only accepts string values so i need to convert the data into string, here is the code · //Convert object to string function convertObjectToStringSingleObject(data) { Object.keys(data).forEach(function (key) { data[key] = String(data[key]); }); return data; }
🌐
AngularJS
docs.angularjs.org › api › ng › function › angular.toJson
API: angular.toJson
AngularJS is what HTML would have been, had it been designed for building web-apps. Declarative templates with data-binding, MVC, dependency injection and great testability story all implemented with pure client-side JavaScript!
🌐
Angular Wiki
angularjswiki.com › pipes › jsonpipe
Angular Json Pipe | Angular Wiki
Angular Json Pipe converts a value or object into JSON formatted string. Usually we will get the data from the server in JSON format, and we will bind it to HTML.
🌐
Medium
medium.com › @nitinsgavane › angular-working-with-json-data-in-different-scenarios-552947e026bc
Angular | Working with JSON Data in Different Scenarios | by Nitin Gavhane | Medium
December 28, 2023 - import { Component } from '@angular/core'; import * as prod from '../Model/Product.json' @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { title = 'JSONDataExample'; products: any = (prod as any).default; constructor(){ console.log(prod); } } ... Angular allows us to fetch JSON data from a remote server using HTTP.
🌐
Runninghill
runninghill.co.za › blog › downloading-objects-as-json-files-in-angular
Downloading Objects as JSON Files in Angular Apps
October 26, 2023 - In this blog post, we’ll explore how to download a session storage object as a JSON file in an Angular application.