Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A JSONObject text must begin with '{' at 1 [character 2 line 1] #1764

Open
GaganVaidya opened this issue Jan 17, 2024 · 5 comments
Open

A JSONObject text must begin with '{' at 1 [character 2 line 1] #1764

GaganVaidya opened this issue Jan 17, 2024 · 5 comments

Comments

@GaganVaidya
Copy link

Code I wrote yesterday:

package APITesting;

import static io.restassured.RestAssured.;
import static io.restassured.matcher.RestAssuredMatchers.
;
import static org.hamcrest.Matchers.*;

import org.json.JSONObject;
import org.testng.annotations.Test;

import io.restassured.http.ContentType;
import io.restassured.response.Response;

public class ParsingResponse {

   @Test
void getBookObjet() {
	
	Response res=given()
		.contentType(ContentType.JSON)
	
	.when()
		.get("http://localhost:3000/Store");
	
	//get key value with JSONObject class
	JSONObject jo=new JSONObject(res.toString());		
	for(int i=0;i<jo.getJSONArray("book").length();i++) 
	{
		String str=jo.getJSONArray("book").getJSONObject(i).get("title").toString();
		System.out.println(str);
	}
	
}

}

File I have used for API testing:
book.json

Response I got in Console:
[RemoteTestNG] detected TestNG version 7.9.0
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
FAILED: APITesting.ParsingResponse.getBookObjet
org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]

===============================================
Default test
Tests run: 1, Failures: 1, Skips: 0

===============================================
Default suite
Total tests run: 1, Passes: 0, Failures: 1, Skips: 0

@GaganVaidya
Copy link
Author

Please give me a solution on it. I am stuck in the JSONObject class.

@GaganVaidya GaganVaidya changed the title Getting org.json.JSONException for Rest Assured Getting org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1] in Rest Assured Jan 18, 2024
@GaganVaidya GaganVaidya changed the title Getting org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1] in Rest Assured Getting A JSONObject text must begin with '{' at 1 [character 2 line 1] in Rest Assured Jan 20, 2024
@GaganVaidya GaganVaidya changed the title Getting A JSONObject text must begin with '{' at 1 [character 2 line 1] in Rest Assured A JSONObject text must begin with '{' at 1 [character 2 line 1] Jan 20, 2024
@lucas-nguyen-17
Copy link
Contributor

lucas-nguyen-17 commented Jan 23, 2024

This is a basic task of extracting value from json, and you're making a simple thing complicated. Rest-Assured has a way to deal with it using JsonPath class.

List<String> titles = new JsonPath(res.asString()).getList("Store.book.title");
System.out.println(titles);
//[CA Rahul, QA Aniket, Anime Anurag, Gamer Gagan]

@GaganVaidya
Copy link
Author

Thanks for suggesting the simplest way to avoid complex code, I will implement this in my framework.

@GaganVaidya
Copy link
Author

@lucas-nguyen-17 could you please give me your opinions on my rest-assured framework (https://github.com/GaganVaidya/APIFramework)

Thanks!

@jobby-tech
Copy link

JSONObject jo=new JSONObject(res.toString());

-> This should have been

JSONObject jo=new JSONObject(res.asString());

asString instead of toString, if you go back to the tutorial video the instructor would have updated it subsequently. :) I was also stuck in the same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants