Skip to content
This repository has been archived by the owner on Dec 4, 2018. It is now read-only.

not able to parse large JSON object with JSON.parse() #109

Open
hishobhit82 opened this issue Sep 27, 2016 · 6 comments
Open

not able to parse large JSON object with JSON.parse() #109

hishobhit82 opened this issue Sep 27, 2016 · 6 comments

Comments

@hishobhit82
Copy link

Hi,

I have a RestFul Service which returns JSON string.

I am using an Electron application which uses NODEJS scripts under the hood. In that code i am calling my a .Net Rest Service and trying to parse the JSON data with JSON.parse(). When JSON strings length is less than ~8500 , It works fine , But when it goes beyond this then I get the below error.

VM156:1 Uncaught SyntaxError: Unexpected token : in JSON at position 6

Like in this example the JSON length was 65000 . Please help to get rid of the error.

My aim is to fetch 100 rows from a table in oracle and display on the form using ELECTRON application.

Thanks & Regards
Shobhit
error1
error2

@Poetro
Copy link

Poetro commented Sep 27, 2016

The issue is, that you parse the data which is just a chunk of the data, not the full response. First you'd have to collect all the data into one array, and then concat and parse it, or stream the whole data to JSONStream instead of parsing it on your own.

@hishobhit82
Copy link
Author

Thanks Poetro for the reply. Could you please elaborate a little bit more as I am completely new to JavaScript NodeJs etc.. like how can i use "JSONStream" .

If you could give me a reference material which I can refer, that would be great.

@Poetro
Copy link

Poetro commented Sep 29, 2016

It would be something like without using JSONStream:

http.request(options, function(res) {
  var buffers = []
  res
    .on('data', function(chunk) {
      buffers.push(chunk)
    })
    .on('end', function() {
      JSON.parse(Buffer.concat(buffers).toString())
    })
})

For using it with JSONStream, it would be something like:

http.request(options, function(res) {
  var stream = JSONStream.parse('*')
  res.pipe(stream)
  stream.on('data', console.log.bind(console, 'an item'))
})

@hishobhit82
Copy link
Author

Thanks a lot mate. Its working like a charm now. You made my day. Thanks again. Also could you refer me a good book to learn node to get upto speed.

@Poetro
Copy link

Poetro commented Sep 30, 2016

There are some good / acceptable books from Packt, like Node.js Design Patterns and Mastering Node.js which I read, but there must be some more up to date ones.

@dominictarr
Copy link
Owner

@Poetro thanks for doing a great job of support on my module, I really appreciate it!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants