JSONException : A JSONObject text must begin with ‘{‘ at 1 [character 2 line 1]

Are you getting error “A JSONObject text must begin with ‘{‘ at 1 [character 2 line 1]” while parsing JSON response. First you need to check whether JSON string/response is valid or invalid JSON.

The following is the error you may get

Exception in thread "main" org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]
	at org.json.JSONTokener.syntaxError(JSONTokener.java:433)
	at org.json.JSONObject.<init>(JSONObject.java:194)
	at org.json.JSONObject.<init>(JSONObject.java:321)
	at com.sneppets.solution.JSONObjectException.main(JSONObjectException.java:15)

For instance, let us consider the following example code in which we are passing valid json string to create JSONObject before parsing and fetching list of user id’s

package com.sneppets.solution;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

public class JSONObjectExample {
	
	public static void main (String[] args) {
		
		String data = "{\"data\":[{\"username\":\"ramu\",\"email\":\"[email protected]\",\"is_enabled\":true,\"_id\":\"123597\"},{\"username\":\"raju\",\"email\":\"[email protected]\",\"is_enabled\":true,\"_id\":\"123598\"}],\"meta\":{\"total\":2,\"references\":{}}}";
		JSONObject jsonObj = new JSONObject(data);
		List<String> idList = getIdList(jsonObj);
		System.out.println("List of user id's : " + idList);
	}

	private static List<String> getIdList(JSONObject jsonObj) {
		List<String> idList = new ArrayList<>();
		JSONArray jsonArr = new JSONArray(jsonObj.get("data").toString());
		if (jsonArr.length() > 0) {
			for (int i=0; i<jsonArr.length(); i++) {
				JSONObject obj = jsonArr.getJSONObject(i);
				idList.add((obj.get("_id").toString()));
			}
		}
		return idList;
	}

}

Output

List of user id's : [123597, 123598]

A JSONObject text must begin with ‘{‘ at 1 – Example

For example, let us pass an invalid JSON as shown in the following sample code which may result in “A JSONObject text must begin with ‘{‘ at 1 [character 2 line 1]” error. For instance, let us remove the starting and ending braces from the json string as shown below and run the below code

package com.sneppets.solution;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

public class JSONObjectException {
	
	public static void main (String[] args) {
		//removed starting and ending braces - resulting invalid JSON
		String data = "\"data\":[{\"username\":\"ramu\",\"email\":\"[email protected]\",\"is_enabled\":true,\"_id\":\"123597\"},{\"username\":\"raju\",\"email\":\"[email protected]\",\"is_enabled\":true,\"_id\":\"123598\"}],\"meta\":{\"total\":2,\"references\":{}}";
		
		JSONObject jsonObj = new JSONObject(data);
		List<String> idList = getIdList(jsonObj);
		System.out.println("List of user id's : " + idList);
	}

	private static List<String> getIdList(JSONObject jsonObj) {
		List<String> idList = new ArrayList<>();
		JSONArray jsonArr = new JSONArray(jsonObj.get("data").toString());
		if (jsonArr.length() > 0) {
			for (int i=0; i<jsonArr.length(); i++) {
				JSONObject obj = jsonArr.getJSONObject(i);
				idList.add((obj.get("_id").toString()));
			}
		}
		return idList;
	}

}

Output

Exception in thread "main" org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]
	at org.json.JSONTokener.syntaxError(JSONTokener.java:433)
	at org.json.JSONObject.<init>(JSONObject.java:194)
	at org.json.JSONObject.<init>(JSONObject.java:321)
	at com.sneppets.solution.JSONObjectException.main(JSONObjectException.java:18)

Therefore, first you need to check whether your JSON is valid or not before creating JSONObject from json string and parsing it.

Further Learning

References

Leave a Reply

avatar
  Subscribe  
Notify of