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.

5
  • 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".) Commented Jul 5, 2022 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. Commented Jul 5, 2022 at 15:28
  • Show the code that causes the bug Commented Jul 5, 2022 at 15:31
  • It's not too long, it's too short. Commented Jul 5, 2022 at 15:34
  • It should not be possible with the native JSON parser. Some third parties libraries should help with reading colossal JSON files. stackoverflow.com/questions/54817985/… Commented Jul 6, 2022 at 0:19

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.