composer.json does not contain valid JSON
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
# Composer Error: Why `composer.json` Fails Validation Even When It Looks Perfect
Running standard development tasks in a Laravel project often involves diving into the dependency management layer, and one of the most fundamental commands is `composer update`. However, sometimes, this process throws cryptic errors that derail productivity. A very common, yet frustrating, error developers encounter is: `[Seld\JsonLint\ParsingException] "./composer.json" does not contain valid JSON`.
You might be in the exact situation described: you validate your `composer.json` file on external tools like [www.jsonlint.com](https://www.jsonlint.com) and it passes inspection, yet Composer refuses to read it. As a senior developer, I can tell you that this discrepancy usually points not to a complex structural error, but to subtle issues related to file encoding or invisible characters that standard linters often overlook.
This post will dive deep into why this happens, how to debug the issue, and ensure your Composer operations run smoothly within your Laravel environment.
---
## The Disconnect: Why External Validators Lie
When a JSON parser fails during execution (like when running `composer update`), it means the file, as read by the operating system or the PHP interpreter executing Composer, does not conform to strict JSON standards.
If an external validator passes the file, it typically means the *textual content* is syntactically correct. However, the failure occurs at runtime because Composer (or the underlying PHP extension) encounters something it cannot interpret as valid UTF-8 or standard JSON characters. The most frequent culprits are:
1. **Byte Order Marks (BOMs):** Files saved with a Byte Order Mark (especially UTF-8 BOMs, often added by certain text editors like Notepad on Windows) introduce invisible bytes at the beginning of the file. These invisible bytes confuse strict parsers.
2. **Invisible Whitespace or Control Characters:** Accidental inclusion of non-standard whitespace, zero-width spaces, or other control characters that are syntactically harmless to a human eye but fatal to a rigid JSON parser.
3. **Encoding Mismatch:** While less common in modern systems, inconsistencies between the file's declared encoding and how PHP reads it can cause parsing failures.
## Practical Solutions: Debugging Your `composer.json`
Since we know the structure looks fine, our debugging focus must shift to the file itself. Here are the practical steps you should take to resolve this error:
### 1. Re-save with Strict UTF-8 Encoding
The most effective fix is ensuring your `composer.json` file is saved using pure, standard UTF-8 encoding *without* any BOM.
**Action:** Open your `composer.json` in a robust code editor (like VS Code, Sublime Text, or Notepad++) and explicitly save the file as **UTF-8 (without BOM)**. Many modern editors offer this option in their "Save As" menu.
### 2. Clean the File Manually
If re-saving doesn't work, you can manually clean up the file to eliminate hidden characters.
**Action:** Copy the content of your valid `composer.json` (perhaps from a freshly generated Laravel project) and paste it into a brand new plain text editor. Then, copy it again and paste it back into your existing file, overwriting the contents entirely. This process effectively strips out any lingering invisible characters.
### 3. Verify File Permissions
Although less likely to cause a JSON parsing error, ensure that the user running Composer has full read access to the file. Check permissions using:
```bash
ls -l composer.json
```
Ensure the relevant user has read (`r`) and write (`w`) permissions on the file.
## Contextualizing with Laravel Ecosystem
When managing dependencies for a framework like Laravel, dependency integrity is paramount. Composer acts as the central nervous system for this process. As you build complex applications, relying on clean, predictable dependency files is crucial for maintainabilityâa core principle seen in modern PHP ecosystem development, much like the standards promoted by [laravelcompany.com](https://laravelcompany.com). A corrupted `composer.json` introduces unnecessary friction that should be avoided at all costs.
## Conclusion
The error `composer.json does not contain valid JSON` when the file appears correct is almost always an issue of file encoding or hidden characters, rather than structural syntax. By systematically re-saving your file in pure UTF-8 format and manually cleaning the content, you will resolve this parsing failure instantly. Always treat configuration files with suspicion, especially when dealing with automated tools; a few invisible bytes can stop the entire build process.