I have a rather long JSON file that I am using https.request to get from a URL. When I run JSON.parse on the string that I receive, I get an "Unexpected end of JSON input" error because it seems like the JSON.parse has a limit to how many characters it can parse and it will cut it off around halfway through my JSON file. Is it possible to somehow parse only half of the string, or retrieve only half of a JSON file from a URL? I am using Javascript.
1 Answer
What you're looking at is at SAX-like parser. First search: is there a SAX parser that reads json and fires events ... or streaming json parser (duplicate)
Basically, when one reads a large file, one has to know if can keep the whole file as a buffer in memory or one can read it in chunks (or per char) and you can interpret it in chunks (as stream).
JSON.parse()does not have a limit. When you say "rather long", how large is the file? (The direct answer to your question is "no".)on('data'callback instead of waiting for the whole request to come back. Don't usehttp.requestif you can avoid it, it is more trouble then it is worth, usefetchinstead.