[SOLVED] Angular-6: Convert string to array of a custom object


I have this produced string:

string str = [{"id":1,"name":"Angular"},{"id":2,"name":"SpringBoot"}]

I’d like to convert it to an array of Objects to have that:

listexps: Expertise[];
listexps = [{"id":1,"name":"Angular"},{"id":2,"name":"SpringBoot"}];

And Expertise class is

export class Expertise
    id: number;
    name: string;

I tried that:

let array = str .replace('[{','').replace('}]','').split("},{").map(String);

but that didn’t resolve my problem, I got:


instead of


Have you please any idea about solving that ?.
Big thanks.


What you need is JSON.parse; it converts string to an object;

relevant ts:

import { Component } from '@angular/core';
export class Expertise {
  id: number;
  name: string;
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
export class AppComponent {
  name = 'Angular';
  strIntoObj: Expertise[];

  constructor() {
    let str: string = '[{"id":1,"name":"Angular"},{"id":2,"name":"SpringBoot"}]';
    this.strIntoObj = JSON.parse(str);

complete working stackblitz here

Answered By – Akber Iqbal

Answer Checked By – David Goodson (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *