You can either use json-formatter-js package

Copyimport JSONFormatter from 'json-formatter-js';

@Directive({
  selector: 'json-formatter'
})
export class JsonFormatterDirective implements OnChanges {
  @Input() json: any;

  constructor(private elRef: ElementRef) { }

  ngOnChanges() {
    if (this.json) {
      const formatter = new JSONFormatter(this.json);
      this.elRef.nativeElement.appendChild(formatter.render());
    }
  }
}

Plunker Example

or create the same component for angular2

Plunker Example

Answer from yurzui on Stack Overflow
🌐
GitHub
github.com › mohsen1 › json-formatter
GitHub - mohsen1/json-formatter: Angular directive for collapsible JSON in HTML
JSON Formatter is an AngularJS directive for rendering JSON objects in HTML with a collapsible navigation.
Starred by 371 users
Forked by 85 users
Languages   JavaScript 85.9% | CSS 8.9% | HTML 5.2% | JavaScript 85.9% | CSS 8.9% | HTML 5.2%
🌐
npm
npmjs.com › package › ngx-json-viewer
ngx-json-viewer - npm
November 21, 2022 - JSON formatter / viewer for Angular. Latest version: 3.2.1, last published: 3 years ago. Start using ngx-json-viewer in your project by running `npm i ngx-json-viewer`. There are 44 other projects in the npm registry using ngx-json-viewer.
      » npm install ngx-json-viewer
    
Published   Nov 21, 2022
Version   3.2.1
Author   Vivo Xu
🌐
npm
npmjs.com › package › jsonformatter
jsonformatter - npm
February 13, 2018 - JSON Formatter is an AngularJS directive for rendering JSON objects in HTML with a collapsible navigation.
      » npm install jsonformatter
    
Published   Feb 13, 2018
Version   0.7.0
🌐
Azimi
azimi.me › json-formatter › demo › demo.html
AngularJS directive demo - json-formatter
JSON Formatter is an AngularJS directive for rendering JSON objects in HTML with a collapsible navigation.
🌐
Plunker
embed.plnkr.co › 6Cbs6pIPqGlXM0hmXF9n
Angular + Json formatter - Plunker
//our root app component import {Component, NgModule, VERSION} from '@angular/core' import {BrowserModule} from '@angular/platform-browser' import { JsonFormatterModule } from './json-formatter/json-formatter.module'; @Component({ selector: 'my-app', template: ` <h2>Json formatter angular2 based on https://github.com/mohsen1/json-formatter </h2> <json-formatter [open]="true" [json]="obj"></json-formatter> `, }) export class App { obj = { numbers: [ 1, 2, 3 ], boolean: true, 'null': null, number: 123, anObject: { a: 'b', c: 'd', e: 'f\"' }, string: 'Hello World', url: 'https://github.com/mohsen1/json-formatter', date: 'Sun Aug 03 2014 20:46:55 GMT-0700 (PDT)', func: function add(a, b) { return a + b; } } } @NgModule({ imports: [ BrowserModule, JsonFormatterModule ], declarations: [ App ], bootstrap: [ App ] }) export class AppModule {}
Find elsewhere
🌐
GitHub
github.com › hivivo › ngx-json-viewer
GitHub - hivivo/ngx-json-viewer: JSON formatter and viewer in HTML for Angular · GitHub
JSON formatter and viewer in HTML for Angular. Contribute to hivivo/ngx-json-viewer development by creating an account on GitHub.
Starred by 182 users
Forked by 71 users
Languages   TypeScript 68.2% | SCSS 13.3% | HTML 9.0% | JavaScript 9.0% | CSS 0.5%
🌐
Angular Script
angularscript.com › home › angular directive for beautiful json formatter
Angular Directive For Beautiful JSON Formatter | Angular Script
December 17, 2014 - JSON Formatter is an AngularJS directive for rendering JSON objects in HTML with a collapsible navigation.
🌐
Angular Wiki
angularjswiki.com › angular › angular-json-pipe-pretty
Angular json pipe pretty format | Angular Wiki
{ "Id": 1, "Name": "Angular wiki" ... json object it’s very difficult to understand. So to format the JSON displayed, use <pre> tag in HTML....
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.

🌐
Angular
v17.angular.io › api › @angular/common › jsonpipe
Angular - JsonPipe
Converts a value into its JSON-format representation. Useful for debugging.
🌐
CoreUI
coreui.io › answers › how-to-format-json-with-angular-json-pipe
How to format JSON with Angular json pipe · CoreUI
December 11, 2025 - Use the json pipe in Angular templates to format objects as readable JSON. // component.ts export class AppComponent { user = { id: 1, name: 'John Doe', email: '[email protected]', address: { city: 'New York', country: 'USA' } } } <!-- template.html ...
🌐
StackBlitz
stackblitz.com › edit › pretty-json
Pretty Json - StackBlitz
Starter project for Angular apps that exports to the Angular CLI
🌐
GitHub
github.com › n3wtron › ngx-json-viewer2
GitHub - n3wtron/ngx-json-viewer2: JSON formatter and viewer in HTML for Angular 2/4/5/6/7+
JSON formatter and viewer in HTML for Angular 2/4/5/6/7+ - n3wtron/ngx-json-viewer2
Author   n3wtron
🌐
Slashdot
slashdot.org › software › application development › json editors › angular
Top JSON Editors for Angular in 2026
The online JSON Editor provided by JSON Formatter is an intuitive tool crafted for the purposes of editing, viewing, and analyzing JSON data efficiently. Among its numerous features are capabilities for formatting, beautifying, and validating JSON, in addition to converting it into other formats ...