title: Rest Assured: How do I return JSON response as a String? (Java)
body: <p>How will I return a JSON response as a String with my code?</p>
  <p><strong>Purpose</strong> : I want to call <code>getAccessToken()</code> after it has obtained the accessToken from a json response body and need to return it as a <code>String</code> to be used in other methods.</p>
  <p>The response example I'm trying to obtain:</p>
  <pre><code>&quot;accessToken&quot; : &quot;The ID I need from here&quot;
  </code></pre>
  <p>Code :</p>
  <pre><code>private String apiAccessToken;
  
  public JsonPath getAccessToken() {
      JsonPath jsonPath = given().header(&quot;X-API-KEY&quot;, Config.API_KEY).header(&quot;session&quot;, this.sessionID)
              .header(&quot;username&quot;, this.userNameId).queryParam(&quot;code&quot;, verifiedCode).log().all()
              .get(baseUri + basePath + &quot;/vm1/verifyCode&quot;).then().log().all().extract().jsonPath();
  
      this.apiAccessToken = jsonPath.get(&quot;accessToken&quot;);
      return new JsonPath(apiAccessToken);
  }
  </code></pre>
  <p>[<em>Added - Showing how I'm using solutions from comments below</em>]</p>
  <p>Example of how I call this method</p>
  <pre><code>public static String getToken(String key) {
      String res = given()
              .header(&quot;X-API-KEY&quot;, Config.API_KEY)
              .header(&quot;session&quot;, this.SessionId)
              .header(&quot;username&quot;, this.UserName)
              .queryParam(&quot;code&quot;, verifiedCode)
              .log().all()
              .get(baseUri + basePath + &quot;/vm1 /verifyCode&quot;)
              .then()
              .log().all()
              .extract().asString();
  
      JsonPath js = new JsonPath(res);
      return js.get(key).toString();
  }
  
  public static String getJsonPath(Response response, String key) {
      String complete = response.asString();
      JsonPath js = new JsonPath(complete);
      return js.get(key).toString();
  }
  
  @Test
      public void testAuthValidator() throws InterruptedException, IOException, GeneralSecurityException {
          String sentCode = GmailUtility.getVerificationCode(); // Uses GMAIL API service to to read and get code from email and sends to getAccessToken
          System.out.println(sentCode);
          String Token = getToken(&quot;accessToken&quot;); // Validates verification code. Spits out response for accessToken
          System.out.println(validator);
          driver = initializeDriver(); // Invokes Chrome
          driver.get(env.API_Website); // Goes to api website
          AuthApi auth = new AuthApi(driver);
          auth.getAuthorizeButton().click(); // Clicks a text field on webpage
          auth.getValueField().sendKeys(&quot;Token &quot; + Token); // Need to call extracted response for the accessToken from getAccessToken.
  </code></pre>