Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
How to convert JSON string to JSON object in angular

How to convert JSON string to JSON object in angular ?

Aman Jain, April 13, 2022November 6, 2023

Sometimes in our application we want to convert JSON string to JSON object because the format of response is JSON string rather then JSON object, so in this tutorial i will show you to use JSON string to object and vise versa. JSON are used to communicate between client and server application.

Angular is most popular frontend framework and not only in angular even in javascript or other framework we ca land into the same problem so to convert JSON string to JSON object in angular here is the simplest solution

Convert string to json

JSON.parse is used to convert the string to json

let jsonString='[{"title":"test"}]';
let jsonObj = JSON.parse(jsonString);

Convert json object to string

JSON.stringify is used to convert the string to json

let jsonObj = [{"title":"test"}];
let jsonString = JSON.stringify(jsonString);

So as you can we have used two methods here

  1. JSON.stringify : Its used to convert object to json
  2. JSON.parse : Its used to convert string object to Object

Let’s take an example of convert JSON string to JSON object in angular

Step 1 : Create an angular app

First step is to setup an angular app using below command

ng new example-app

Step 2: Create a Component

Create a component and add json stringify and parse in component

ng g c hello-world
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

@Component({
  selector: 'app-hello-world',
  templateUrl: './hello-world.component.html',
  styleUrls: ['./hello-world.component.css'],
})
export class HelloWorldComponent implements OnInit {
  jsonString: string = '[{"title":"test"},{"title":"test2"}]';
  jsonObj: Array<object>;
  jsonObj2: Array<object> = [{ title: 'test' }, { title: 'test2' }];
  constructor() {
    this.jsonObj = JSON.parse(this.jsonString);
    this.jsonString = JSON.stringify(this.jsonObj2);
  }

  ngOnInit() {}
}

Here we used to convet the string to object and object to string.

Step 3 : Create template and add html

Now, add simple html template with a *ngFor

<h2>JSON STRING TO OBJECT</h2>

<div *ngFor="let obj of jsonObj">
  {{ obj.title }}
</div>

<h2>JSON OBJECT TO STRING</h2>

{{ jsonString }}

Live Example and code:

Related

Javascript Angular angularjsonparse

Post navigation

Previous post
Next post

Related Posts

Javascript How to use local storage in angular

How to use local storage in angular ?

April 18, 2022October 4, 2022

Local storage are used to store client side data in browser and in this tutorial we will learn to use local storage in angular. Angular is a javascript framework and used to enable to seamless developer experience to build web and mobile applications. Local storage are useful when we want…

Read More
Javascript Arrow function

What is arrow functions in JavaScript / ES6?

June 12, 2021November 8, 2023

arrow functions in JavaScript are introduced in ES6, before es6 we used normal function which needs to write more code to achieve the functionality. let take an example of arrow functions and es5 normal functions are follow: Normal functions of es5 Arrow functions of es6 uhmm! its looking like so…

Read More
Javascript How to create global variable in angular ?

How to create global variables in angular ?

October 3, 2022March 16, 2024

In this article, we will learn about the create global variables in Angular and how to use them. We will also see how to use them across the application by importing the file. There are different ways to create global variables in angular. One way is to use the global…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version