-1

I am getting this raw output from Twitter API stream

<Buffer 7b 22 64 61 74 61 22 3a 7b 22 65 64 69 74 5f 68 69 73 74 6f 72 79 5f 74 77 65 65 74 5f 69 64 73 22 3a 5b 22 31 35 38 38 32 31 37 34 39 32 37 34 30 30 ... 349 more bytes>

I am trying to convert it to JSON like this

const obj = JSON.parse(data);
console.log(obj)

But I am getting this error

SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at Request.<anonymous> (/home/scooprank/Github/scooprank/tweet_listener/server.js:63:26)
    at Request.emit (node:events:513:28)
    at IncomingMessage.<anonymous> (/home/scooprank/Github/scooprank/node_modules/request/request.js:1073:12)
    at IncomingMessage.emit (node:events:513:28)
    at addChunk (node:internal/streams/readable:315:12)
    at readableAddChunk (node:internal/streams/readable:289:9)
    at IncomingMessage.Readable.push (node:internal/streams/readable:228:10)
    at HTTPParser.parserOnBody (node:_http_common:140:24)
    at TLSSocket.socketOnData (node:_http_client:521:22)

How do I resolve this?

user3840170
  • 26,597
  • 4
  • 30
  • 62
Nabeel Hassan
  • 149
  • 1
  • 1
  • 9
  • 1
    Does this answer your question? [Converting between strings and ArrayBuffers](https://stackoverflow.com/questions/6965107/converting-between-strings-and-arraybuffers) – Tibrogargan Nov 03 '22 at 17:20
  • No, I am unable to convert the raw data to Json – Nabeel Hassan Nov 03 '22 at 17:51
  • Yes. Your raw data is not a string. It's buffer containing bytes. You need to create a ByteArray from it and then convert that to a string before you attempt to parse it as JSON. – Tibrogargan Nov 03 '22 at 18:05
  • Actually, @Tibrogargan It is a string but in binary form... parsing it as UTF-8 yields the begining of a JSON string. – Dr. Vortex Nov 03 '22 at 19:16
  • @Dr-Vortex a "string in binary form" is just another way of saying "a buffer containing bytes". Except that technically binary would be a string of 1s and 0s, not hex values. – Tibrogargan Nov 03 '22 at 19:23
  • The point I'm trying to make is that the buffer, when decoded with UTF-8 is valid JSON. The OP needs said JSON. Hence converting the buffer to text would give the OP the JSON string they need. – Dr. Vortex Nov 03 '22 at 19:25
  • @dr-vortex You mean... the point I made already when I marked the question as a duplicate of a question asking how to convert a buffer of bytes to a string? – Tibrogargan Nov 03 '22 at 19:29

1 Answers1

0

assign the buffer to a variable, them use the method of the buffer to transfer to a JSON, something like this

let url = 'something'

let obj: Buffer = Buffer.from(url)

let obj2 = obj.toJSON()