I'm trying to validate a big piece of JSON
in PHP
.
There is no need to use the actual data, I just need to validate it.
I can not use json_decode
, because it goes over the memory limit.
What would be the best way to do it?
I'm trying to validate a big piece of JSON
in PHP
.
There is no need to use the actual data, I just need to validate it.
I can not use json_decode
, because it goes over the memory limit.
What would be the best way to do it?
I expect you could count the control structures (brackets and braces) and if the number of open/close structures are identical, you could make a partial bet that the JSON was OK. There are lots of ways it could still be wrong, like improper nesting, but I think the only really good way of validating the string would be to decode it. You might look into raising the memory limit, or having the author of the JSON send subsets so that you can decode the subsets and validate each partial component of the object.
Here is a discussion about using a regexp for validation(without parse the content) Regex to validate JSON
The right way to do this is to build a parser. Check out this article or do some research on the process of building a parser. Basically, that means defining a grammar for JSON (which is pretty much done for you at http://JSON.org) and then building functions to serve as checks for the corresponding machines to process each type of token. This will reduce the amount of memory required to do the processing (in an ideal implementation it could probably be down to the size of the largest JSON value).
It may be worthwhile to do some searching on Google to make sure you are not reinventing the wheel.