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);
Answer from rodrigocfd 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>"
});
🌐
Cloudhadoop
cloudhadoop.com › home
How to convert json to/from an object in angular|Typescript
December 31, 2023 - JSON.stringify() method string version of an object, which is the conversion of an object to JSON string · JSON.parse() - parse string JSON object and creates javascript object · import { Component, OnInit } from "@angular/core"; @Component({ ...
🌐
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
🌐
GeeksforGeeks
geeksforgeeks.org › angularjs › angularjs-angular-tojson-function
AngularJS angular.toJson() Function - GeeksforGeeks
July 11, 2025 - Example 1: This example describes ... where <fieldset> tag is utilized to group the related elements in the form, and it creates the box over the elements, in order to serialize the javascript object into a JSON string format...
Find elsewhere
🌐
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 - } In this example, we are going to parse the previously created JSON data into object format using JSON.parse() method.
🌐
Angular Wiki
angularjswiki.com › pipes › jsonpipe
Angular Json Pipe | Angular Wiki
export class AppComponent { product : Product; constructor(){ this.product = {Id:1,Name:'Angular wiki'}; } name = 'Angular ' + VERSION.major; } export class Product{ Id : Number; Name: string; } In the component HTML file, I am displaying product ...
🌐
W3Schools
w3schools.com › angularjs › ng_filter_json.asp
Angular json Filter
<div ng-app="myApp" ng-control... "country" : "Germany" }; }); </script> Try it Yourself » · The json filter converts a JavaScript object into a JSON string....
🌐
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!
🌐
Stack Overflow
stackoverflow.com › questions › 42909258 › json-to-object-deeply
angular - Json to Object deeply - Stack Overflow
I try to parse a Json into a ClassA instance like this: let classA: ClassA = Object.assign(new ClassA(), JSON.parse('{"name":"classA", "classB": {"name":"classB"}}'));
🌐
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 - Angular provides versatile ways to handle JSON data, whether it’s sourced locally, retrieved via HTTP, or fetched from a MySQL database. ... Reading JSON data from a local file is a common practice, and Angular makes it straightforward. Let’s set up a simple example:
🌐
Angular
v17.angular.io › api › @angular/common › jsonpipe
Angular - JsonPipe
The following component uses a JSON pipe to convert an object to JSON format, and displays the string in both formats for comparison.
🌐
DEV Community
dev.to › rramname › read-properties-of-json-object-and-its-values-dynamically-in-angular-javascript-3db2
Read properties of Json object and its values dynamically in Angular/JavaScript - DEV Community
March 29, 2021 - We also have a property called headers in which we will be deriving the headers from the input object. The entire magic of this post is this line of code ... This is not a feature of angular. This is the feature of Javascript, I am just using it in my angular code. Here we are taking first element from the Json array and getting all keys from it.
🌐
SSOJet
ssojet.com › serialize-and-deserialize › serialize-and-deserialize-json-in-angular
Serialize and Deserialize JSON in Angular | Serialize and Deserialize Data in Programming Languages
October 24, 2025 - This guide walks you through effectively serializing and deserializing JSON data within your Angular projects. You'll learn how to transform raw JSON into strongly-typed TypeScript objects and vice-versa, ensuring data integrity and simplifying your application logic.