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:
and this is a JSON string:
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:
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:
| Error Cause | Description | Solution |
| Input Data Mismatch | Expected JSON object but received a string | Validate the input data format |
| API Response | Server response is not in the expected JSON format | Inspect and validate API endpoint responses |
| File Content | Incorrectly formatted content in the file as JSON | Verify file contents and ensure JSON structure |
| Parsing Code Issues | Code expects wrong format or lacks error checking | Implement robust error handling and type checks |
Related reading
- Expiring 1 record(s) for xxxxx 30030 ms has passed since batch creation plus linger time
- Expiry Policy on Off Heap entries not working as expected in Ignite
- Explain which gitignore rule is ignoring my file
- ExpressJS - Elastic Beanstalk 502 Bad Gateway
- Extension exists but uuid_generate_v4 fails
- Extremely small or NaN values appear in training neural network
- F Async.FromBeginEnd not catching exceptions
- Facebook Prophet error cmdstanpy - ERROR - Chain 1 error terminated by signal 11 Unknown error -11
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseInterview Questions practice on Codemia
Over 11,000 interview practice questions for 300+ companies, searchable by company and role.