title: How to convert an object to JSON in Angular
body: <p>Found some answers for angularjs like: <a href="https://stackoverflow.com/questions/11819301/how-to-use-angular-tojson-on-a-angular-controller-or-scope">How to use angular.toJson on a angular controller or scope</a> but not Angular 2 and following.</p>
  
  <p>I'm new to Angular, worked through the tutorial, and now trying to build my first live app. I have a credentials object that has fields username and password. I want to externalize this to JSON to send to my web service. I found this: <a href="https://angular.io/api/common/JsonPipe" rel="nofollow noreferrer">https://angular.io/api/common/JsonPipe</a> which seems to do what I want, but the example is in HTML and I want to do it in my service, so here's my service:</p>
  
  <pre><code>import { HttpClient, HttpHeaders } from '@angular/common/http';
  import { Injectable } from '@angular/core';
  import { JsonPipe } from '@angular/common';
  import { Observable } from 'rxjs/Observable';
  import { of } from 'rxjs/observable/of';
  
  @Injectable()
  export class LoginService {
    authToken = ""
    loginUrl = "localhost:8093/login"
  
    constructor(private http: HttpClient) { }
  
    login(credentials): Observable&lt;String&gt; {
      var url = this.loginUrl
                  + '?payload='
                  + (credentials | json)
      return of(url);
    }
  }
  </code></pre>
  
  <p>But I get an error on the line + <code>(credentials | json)</code> saying json is not found, maybe I meant JSON?</p>
  
  <p>Did I?</p>