I would like to add an even simpler way to do this, using the built-in json pipe:

<pre>{{data | json}}</pre>

This way, the formatting is preserved.

Answer from Shane Hsu on Stack Overflow
๐ŸŒ
Angular
angular.dev โ€บ api โ€บ common โ€บ JsonPipe
JsonPipe โ€ข Angular
The following component uses a JSON pipe to convert an object to JSON format, and displays the string in both formats for comparison.
Top answer
1 of 5
514

I would like to add an even simpler way to do this, using the built-in json pipe:

<pre>{{data | json}}</pre>

This way, the formatting is preserved.

2 of 5
34

I had required this scenario and many times require it. I saw this question is still trending in 2021. So I created a detailed post explaining not how to just prettify it but add colors to it and built a small tool to play around with.

2021+ solution: I built my own custom version of pipe (inspried by this answer) which not only prettifies but also adds colors to JSON like vscode. I don't use a built-in JSON pipe because it doesn't serve my full purpose.

This also gives you the freedom to add number lines and padding if you wish to.

Sample output like below

global stylesheet should contain colors as per your theme for e.g styles.scss

pre {
  font-weight: 400;

  .number-line {
    color: #adadaf;
  }
  .string {
    color: #95c602;
  }
  .number {
    color: #f2b619;
  }
  .boolean {
    color: #0097f1;
  }
  .null {
    color: #727990;
  }
  .key {
    color: #fff;
  }
}

Source code of the pipe

@Pipe({
  name: 'prettyjson',
  pure:true
})
export class PrettyJsonPipe implements PipeTransform {
  transform(value: any, args: any[]): any {
    try {
      /**
       * check and try to parse value if it's not an object
       * if it fails to parse which means it is an invalid JSON
       */
      return this.applyColors(
        typeof value === 'object' ? value : JSON.parse(value),
        args[0],
        args[1]
      );
    } catch (e) {
      return this.applyColors({ error: 'Invalid JSON' }, args[0], args[1]);
    }
  }

  applyColors(obj: any, showNumeberLine: boolean = false, padding: number = 4) {
    // line number start from 1
    let line = 1;

    if (typeof obj != 'string') {
      obj = JSON.stringify(obj, undefined, 3);
    }

    /**
     * Converts special charaters like &, <, > to equivalent HTML code of it
     */
    obj = obj.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
    /* taken from https://stackoverflow.com/a/7220510 */

    /**
     * wraps every datatype, key for e.g
     * numbers from json object to something like
     * <span class="number" > 234 </span>
     * this is why needed custom themeClass which we created in _global.css
     * @return final bunch of span tags after all conversion
     */
    obj = obj.replace(
      /("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g,
      (match: any) => {
        // class to be applied inside pre tag
        let themeClass = 'number';
        if (/^"/.test(match)) {
          if (/:$/.test(match)) {
            themeClass = 'key';
          } else {
            themeClass = 'string';
          }
        } else if (/true|false/.test(match)) {
          themeClass = 'boolean';
        } else if (/null/.test(match)) {
          themeClass = 'null';
        }
        return '<span class="' + themeClass + '">' + match + '</span>';
      }
    );

    /**
     * Regex for the start of the line, insert a number-line themeClass tag before each line
     */
    return showNumeberLine
      ? obj.replace(
          /^/gm,
          () =>
            `<span class="number-line pl-3 select-none" >${String(line++).padEnd(padding)}</span>`
        )
      : obj;
  }
}

now pass these params inside HTML like this. If you don't pass it by default value of showNumberline is false and padding is 4

<pre [innerHTML]="dummyJsonObject | prettyjson: [true, 3]"></pre>

Hope this helps.

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ angularjs โ€บ angular10-jsonpipe
Angular10 JsonPipe - GeeksforGeeks
July 23, 2025 - In app.component.html use the above syntax with '|' symbol to make JsonPipe element. serve the angular app using ng serve to see the output
๐ŸŒ
Angular
angular.dev โ€บ guide โ€บ templates โ€บ pipes
Pipes โ€ข Angular
Pipes are a special operator in Angular template expressions that allows you to transform data declaratively in your template. Pipes let you declare a transformation function once and then use that transformation across multiple templates.
๐ŸŒ
CoreUI
coreui.io โ€บ answers โ€บ how-to-format-json-with-angular-json-pipe
How to format JSON with Angular json pipe ยท CoreUI
December 11, 2025 - The json pipe transforms any JavaScript object into a formatted JSON string with proper indentation, making nested structures easy to read. Wrapping the output in a <pre> tag preserves the whitespace and formatting.
๐ŸŒ
Angular Training
angulartraining.com โ€บ home โ€บ debugging โ€บ debugging with the json pipe
Debugging with the JSON pipe | Angular Newsletter
October 21, 2024 - We have all used console.log at some point to debug our code. With Angular, there is an interesting alternative to display debugging information on our web page temporarily: The JSON pipe. The primary usage is as follows: myData | json The above code will output your data as a JSON string in the span element,
Find elsewhere
๐ŸŒ
Medium
blog.angulartraining.com โ€บ 3-angular-pipes-you-should-now-about-f635d6ab891f
3 Angular pipes you should know about | by Alain Chautard | Angular Training
November 26, 2018 - With Angular 6.1 was released a new pipe: KeyValuePipe. In this article, Iโ€™m going to cover this new pipe as well as two others that can be used for multiple purposes. Letโ€™s start with a very cool pipe that makes debugging a breeze: The JSON pipe.
๐ŸŒ
Medium
medium.com โ€บ swlh โ€บ how-does-json-pipe-work-in-angular-2c7ae7432232
How Does JSON Pipe Work in Angular? | by Konda Reddy Yaramala | The Startup | Medium
July 10, 2020 - In this article, I would like to walk you through the implementation of this simple JsonPipe. With no further wait, letโ€™s get into the source code: ... Source code: https://github.com/angular/angular/blob/10.0.2/packages/common/src/pipes/json_pipe.ts#L10-L34
๐ŸŒ
Angular
v2.angular.io โ€บ docs โ€บ ts โ€บ latest โ€บ api โ€บ common โ€บ index โ€บ JsonPipe-pipe.html
JsonPipe - ts - API
@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]}}; } exported from @angular/common/index defined in @angular/common/src/pipes/json_pipe.ts
๐ŸŒ
Reddit
reddit.com โ€บ r/angular2 โ€บ angular 2/ionic 2 page returns [object object] unless use json pipe
r/Angular2 on Reddit: Angular 2/Ionic 2 page returns [object Object] unless use json pipe
August 27, 2017 -

Some of the data returned from HTTP GET looks like following:

some_data:{availability: "Available", state: "Enabled", reason: "Available"}

I've only been able to get this data to disply by using json pipe. I donโ€™t want to display json on page and would like to know the best way to resolve this. When searching online I see a ton of different approaches to this. Is there a best practice to resolve this in Angular 2?

Example HTML that outputs json on page:

<ion-list>
   <ion-item *ngFor="let item of items">
     <ion-label text-wrap>{{item.some_data | json}}</ion-label>
   </ion-item>
 </ion-list>
๐ŸŒ
Angular
angular.dev โ€บ reference โ€บ configs โ€บ file-structure
File structure โ€ข Angular
The first explicitly generated application goes into the projects folder along with all other projects in the workspace. Newly generated libraries are also added under projects. When you create projects this way, the file structure of the workspace is entirely consistent with the structure of the workspace configuration file, angular.json.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ angularjs โ€บ how-to-transforms-json-object-to-pretty-printed-json-using-angular-pipe
How to transforms JSON object to pretty-printed JSON using Angular Pipe ? - GeeksforGeeks
July 23, 2025 - Example 2: This is another example, we will pass JSON object which has nested JSON object in it. ... <!-- app.component.html --> <h2 style="color: green">GeeksforGeeks</h2> <h2> Angular 2 pipe that transforms JSON object to pretty-printed JSON </h2> <pre [innerHTML]="gfg | pretty"></pre>
๐ŸŒ
Angular Wiki
angularjswiki.com โ€บ pipes โ€บ jsonpipe
Angular Json Pipe | Angular Wiki
Angular Json Pipe converts a value or object into JSON formatted string
๐ŸŒ
ConcretePage
concretepage.com โ€บ angular-2 โ€บ angular-2-json-pipe-example
Angular Json Pipe - ConcretePage.com
February 12, 2024 - JsonPipe is an angular PIPE. JsonPipe converts an expression value into JSON string. JsonPipe uses JSON.stringify to convert into JSON string. In JSON.stringify, JSON is a subset of JavaScript.
๐ŸŒ
Experts PHP
expertsphp.com โ€บ how-can-i-use-json-pipe-in-angular-17
How Can I Use JSON Pipe in Angular 17? - Experts PHP
March 4, 2024 - @Component({ selector: 'app-my-component', templateUrl: './my-component.component.html', styleUrls: ['./my-component.component.css'] }) export class MyComponent { data: any = { name: 'John Doe', age: 30, city: 'New York' }; constructor(private jsonPipe: JsonPipe) {} getJsonString() { return this.jsonPipe.transform(this.data); } } 3. Use the pipe in your template: In your component's template, use the pipe syntax alongside the value you want to convert to JSON:
๐ŸŒ
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!
๐ŸŒ
Juri
juri.dev โ€บ blog โ€บ 2016 โ€บ 09 โ€บ ng2-serialize-with-json-pipe
Angular: Using the JSONPipe for debugging - juri.dev
September 20, 2016 - A powerful way of debugging, especially templates, in Angular 1 was the JSON pipe (or filter) which could be used within a template. The pipe still natively exists in Angular.
๐ŸŒ
Telerik
telerik.com โ€บ blogs โ€บ angular-basics-built-pipes-examples-each
Angular Basics: Built-in Pipes with Examples of Each
June 12, 2023 - I18PluralPipe lets us get the plural version of a word for a given locale and display it. I18nSelectPipe returns a value that corresponds to a given string and shows that. JsonPipe lets us show JSON objects on the screen, which is helpful for ...