0

I'm making a get request in node.js to a url which returns an object i'm trying to parse. However I am getting the unexpected token error. I have played with different encodings as well as converting the response body into string and then removing those tokens but nothing works. Setting encoding to null also did not solve my problem.

Below is the response body im getting:

��[{"unit":"EN15","BOX":"150027","CD":"12 - Natural Gas Leak","Levl":"1","StrName":"1000 N Madison Ave","IncNo":"2020102317","Address":"1036 N Madison Ave","CrossSt":"W 5TH ST/NECHES ST"},{"unit":"EN23","BOX":"230004","CD":"44 - Welfare Check","Levl":"1","StrName":"S Lancaster Rd / E Overton Rd","IncNo":"2020102314","Address":"S Lancaster Rd / E Overton Rd","CrossSt":""}]

Those are the headers which go with my request

headers: {'Content-Type': 'text/plain; charset=utf-8'}

Here is how I'm parsing response body

const data = JSON.parse(response.body)

Any help would be greatly appreciated!

UPDATE: had to do this with response body to make it work

const data = response.body.replace(/^\uFFFD/, '').replace(/^\uFFFD/, '').replace(/\0/g, '')
Matthew
  • 411
  • 6
  • 22

1 Answers1

0

You are probably getting byte order mark (BOM) of the UTF-8 string.

The simplest workeround would be to remove it before the parsing.

const data = JSON.parse(response.body.toString('utf8').replace(/^\uFFFD/, ''));

Update: Your first 2 characters are Unicode REPLACEMENT CHARACTER. In order to remove it, use the \uFFD character.

JeremyRock
  • 396
  • 1
  • 8
  • Yeah i've tried that and still getting the same result – Matthew May 14 '20 at 10:55
  • that sort of solved the problem but now im getting an error that there is a null value - Unexpected token \u0000 in JSON at position 1","at JSON.parse. I have resolved this using JSON.parse(JSON.stringify(data)), however then i cannot access individual elements of the response – Matthew May 14 '20 at 13:34
  • ok this worked for me - const data = response.body.replace(/^\uFFFD/, '').replace(/^\uFFFD/, '').replace(/\0/g, ''). Thanks for your help @JeremyRock – Matthew May 14 '20 at 14:06