JSON Parsing Error
Data Handling
Programming
Error Message
Troubleshooting

Expected BEGIN_OBJECT but was STRING at line 1 column 1 (path $)

Codemia Knowledge Hub ·

"Expected BEGIN_OBJECT but was STRING at line 1 column 1" means the JSON parser expected the data to start with a JSON object ({) but found a string instead. The input you are parsing is not the object your code expects: often it is a bare JSON string, plain text or HTML returned by an API.

Objects versus strings in JSON

JSON (JavaScript Object Notation) is a lightweight data-interchange format, easy for people to read and write and for machines to parse and generate. Data is usually described with two structures:

  • Objects: unordered collections of key/value pairs { }.
  • Arrays: ordered lists of values [ ].

JSON parsing libraries raise this message when the type they expect doesn't match what they find. For example, this is a JSON object:

json
1{
2  "name": "John Doe",
3  "age": 29
4}

and this is a JSON string:

json
"John Doe"

If the code expects the first and receives the second, it throws the error.

Why the data is a string

1. Input data mismatch

The code expects a JSON object, but the input source supplies a JSON string.

2. API response

You expect an application/json response, but a misconfiguration or a server-side problem returns a plain string or HTML instead.

3. File content

A JSON file contains a single string rather than a structured JSON object.

How to fix it

1. Validate the input

Check that the input really is the JSON you expect, using a JSON validator or lint tool.

2. Inspect the API response

If the data comes from an API, look at the raw response with Postman or cURL and confirm its format matches what your code expects.

3. Check the files

Make sure any file read as JSON actually contains a JSON object, and avoid encoding problems (for example UTF-8 vs. ASCII) that can change the content.

4. Make the parsing code defensive

Handle cases where an object or array is implied but the format isn't consistent. Add error handling and data type checks.

Example: parsing a user response

A service that consumes an external API expects a JSON object with user data and parses the response like this:

java
1import com.google.gson.Gson;
2
3class User {
4    String username;
5}
6
7String response = "Unauthorized"; // Example response
8User user = new Gson().fromJson(response, User.class);

This fails because response is a plain string, not a JSON object. Gson throws JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $. With a proper object it works:

java
String response = "{\"username\": \"John Doe\"}"; // Corrected response
User user = new Gson().fromJson(response, User.class);
Error CauseDescriptionSolution
Input Data MismatchExpected JSON object but received a stringValidate the input data format
API ResponseServer response is not in the expected JSON formatInspect and validate API endpoint responses
File ContentIncorrectly formatted content in the file as JSONVerify file contents and ensure JSON structure
Parsing Code IssuesCode expects wrong format or lacks error checkingImplement robust error handling and type checks

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Interview Questions practice on Codemia

Over 11,000 interview practice questions for 300+ companies, searchable by company and role.

Browse interview questions