0

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.

  • `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".) – Pointy Jul 05 '22 at 15:27
  • 1
    This sounds like you are trying to parse in the `on('data'` callback instead of waiting for the whole request to come back. Don't use `http.request` if you can avoid it, it is more trouble then it is worth, use `fetch` instead. – Quentin Jul 05 '22 at 15:28
  • Show the code that causes the bug – epascarello Jul 05 '22 at 15:31
  • It's not too long, it's too short. – IT goldman Jul 05 '22 at 15:34
  • It should not be possible with the native JSON parser. Some third parties libraries should help with reading colossal JSON files. https://stackoverflow.com/questions/54817985/how-to-parse-a-huge-json-file-without-loading-it-in-memory – Patrice Thimothee Jul 06 '22 at 00:19

1 Answers1

0

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).

azbarcea
  • 3,323
  • 1
  • 20
  • 25