question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Handling a BOM with .json()

See original GitHub issue

Anybody know how I should be handling a BOM with a returned json payload? Right now i’m getting this error when calling res.json() Exception: FetchError: invalid json response body at https://api_that_returns_json_with_bom reason: Unexpected token ? in JSON at position 0

I tried adding an Accept header of application/json; charset=utf-8 but that didn’t resolve the issue.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
github-actions[bot]commented, Mar 11, 2022

🎉 This issue has been resolved in version 3.2.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

1reaction
jimmywartingcommented, Jun 21, 2021

Hmm, chrome seems to be able to decode BOM

await new Response(String.fromCharCode(0xFEFF) + '1').json() // 1

What we are essentially doing is:

const buffer = Buffer.from(String.fromCharCode(0xFEFF) + '1'
JSON.parse(buffer.toString()) // Error

We should stop using buffer and turn towards using TextDecoder instead, this removes the BOM

JSON.parse(new TextDecoder().decode((Buffer.from(String.fromCharCode(0xFEFF) + '1')))) // 1

While we are at it:

async text() {
  let text = ''
  const decoder = new TextDecoder()
  for await (const uint8 of this.body) {
    text += decoder.decode(uint8, { stream: true })
  }
  text += decoder.decode() // flush
  return text
}

json() {
  return this.text().then(JSON.parse)
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

The Curious Case of the JSON BOM - Jimmy Bogard
1. Bind the parameter type as 'string' instead of 'SaySomething' to get the raw values and avoid JSON deserialization, or 2. Change the...
Read more >
JSON Specification and usage of BOM/charset-encoding
You are right. The BOM character is illegal in JSON (and not needed); The MIME charset is illegal in JSON (and not needed...
Read more >
Handling a BOM with .json() · Issue #541 · node-fetch ... - GitHub
Anybody know how I should be handling a BOM with a returned json payload? Right now i'm getting this error when calling res.json()...
Read more >
Unicode Character problems in JSON and playing with BOM ...
Here we go! Let's start our article with a question. Is it enough to set “application / json; charset = utf-8” as the...
Read more >
Python : How to fix Unexpected UTF-8 BOM error when using ...
If the json.loads() method throws an Unexpected UTF-8 BOM error, it is due to a BOM value being present in the stream or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found