instead of in your code:

...
this.searchResult = profileResponse;
...

you can use:

searchResult: any[];

...
this.searchResult = profileResponse.json() || {}
...

then you can access each property via searchResult item:

this.searchResult[0].profileId, etc
Answer from Joseph Wu on Stack Overflow
๐ŸŒ
Quora
quora.com โ€บ How-can-I-parse-a-JSON-array-into-an-array-of-objects-in-Angular2
How to parse a JSON array into an array of objects in Angular2 - Quora
Answer (1 of 5): Assuming you are not already receiving Json, and text instead; you can use JSON.parse() to create an object from a properly formatted json string. This can be done in Javascript/Typescript in general, and is not limited to Angular. For example (see line 6 below): [code]var xmlht...
Discussions

Parsing Json Array in AngularJS - Stack Overflow
Communities for your favorite technologies. Explore all Collectives ยท Ask questions, find answers and collaborate at work with Stack Overflow for Teams More on stackoverflow.com
๐ŸŒ stackoverflow.com
August 3, 2016
Convert JSON file to Typescript Array of Objects

The interface has an iD field and the json has a date field. They need to be the same types. You also might need to use JSON.parse(stringValue) depending on how you are getting the JSON data.

More on reddit.com
๐ŸŒ r/Angular2
7
3
June 26, 2020
typescript - How to parse the Json array from Json object using angular - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... In angular application I have called the API and read the Json object inconsole .But inside the Json object there is string and array.Now I have to parse the array from the object incosole. More on stackoverflow.com
๐ŸŒ stackoverflow.com
August 24, 2020
Parse JSON file with arrays using AngularJS - Stack Overflow
You can't iterate over your JSON since it represented not as array but Object. More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
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 used push() but it is not working. please help me.
๐ŸŒ
YouTube
youtube.com โ€บ tutus funny
How to read Json Array Angular - YouTube
How to read Json Array AngularDownload the Learn Programming Apps on Google Playhttps://bit.ly/3QeAQhB
Published ย  September 27, 2022
Views ย  1K
๐ŸŒ
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 - It takes the data in JSON format and converts it into JavaScript Object format. Just see the simple example below. ... } In this example, we are going to parse the previously created JSON data into object format using JSON.parse() method.
Find elsewhere
๐ŸŒ
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({ selector: "my-app", templateUrl: "./app.component.html", styleUrls: ["./app.component.css"], }) export class AppComponent implements OnInit { name = "Angular"; stringJson: any; stringObject: any; courseList = [ { courseid: "1", coursename: "Java programming", author: "Franc", }, { courseid: "2", coursename: "Learn Typescript programming", author: "John"
๐ŸŒ
ConcretePage
concretepage.com โ€บ questions โ€บ 544
How to fetch value from the nested json array in angular?
I want to fetch the value from the nested json array in angular2. I want to fetch the client_id from the following json array: ... Can anyone help me out. ... Thank you. This is worked. ... Thank you so much. I have searched a lot for the solution for the whole day. Finally, found here. So glad. Great work. Keep it up! Thanks again! ... I am a software developer and admin of ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ angularjs โ€บ how-to-loop-through-array-of-json-object-with-ngfor-in-angular
How to Loop through array of JSON object with *ngFor in Angular ? - GeeksforGeeks
July 23, 2025 - These JSON objects are in the form of key-value pairs and are also present in nested JSON format. In this article, we will learn How to iterate over these Array of JSON objects/ nested JSON objects using ngFor Directive in Angular.
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 636805 โ€บ languages โ€บ AngularJS-Parsing-JSON-Object-AngularJS
AngularJS : Parsing JSON Object inside AngularJS controller (HTML Pages with CSS and JavaScript forum at Coderanch)
In order to stay independent of the browser implementation of the window JSON object, JSON objects are serialized in AngularJS with angular.toJson. JSON strings are de-serialized with angular.fromJson. A JSON object de-serialized with JSON.parse isn't necessarily of type "JSON" - so Object.prototype.toString.call(jsonObject) will return "[object Object]" rather than "[object JSON]".
๐ŸŒ
Reddit
reddit.com โ€บ r/angular2 โ€บ convert json file to typescript array of objects
r/Angular2 on Reddit: Convert JSON file to Typescript Array of Objects
June 26, 2020 -

I am reading in a JSON file and attempting to convert them to an array of objects. I get a syntax error on the component file.

This is the component code:

import * as blogs from '../story/stories.json';
import { IStory } from '../models/story';

@Component({
  selector: 'app-story',
  templateUrl: './story.component.html',
  styleUrls: ['./story.component.scss']
})
export class StoryComponent implements OnInit {
  stories: IStory[] ;
  constructor() { }

  ngOnInit() {
    this.stories = blogs; <-- syntax error on this line
      
  }

this is the story.ts file:

export interface IStory {
    id: number;
    author: string;
    title: string;
    pictureUrl: string;
    body: string;
  }

This is the json file, stories.json

[
{"date":3,"author":"AVC", "pictureUrl":"Scorching", "title":"Scorching", "body":"Scorching"},
{"date":4,"author":"ADF", "pictureUrl":"Scorching", "title":"Scorching", "body":"Scorching"}
]
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 63555337 โ€บ how-to-parse-the-json-array-from-json-object-using-angular
typescript - How to parse the Json array from Json object using angular - Stack Overflow
August 24, 2020 - But I have to parse latlong array from the object. ... mapping(token){ let httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json', 'Authorization': 'Token ' + token }) }; //this.http.get(this.url).subscribe( this.http.get(environment.apiurl +'/api/data/json', httpOptions).subscribe( (dronesdt:any)=>{ localStorage.setItem("dronesdt",JSON.stringify(dronesdt)); console.log("Drones",JSON.parse(localStorage.getItem("dronesdt"))); } ) }
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>"
});
๐ŸŒ
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]}}; }