Android: How to parse JSON file with Gson Library?

What is a good library to parse invalid JSON?

  • I am working with legislative data, which sometimes is produced without valid keys or with other problems.  I need a library that has an option not to use "strict" parsing. Like BeautifulSoup for X/HTML.

  • Answer:

    I'm not aware of anything that can parse invalid structured data such as JSON. And that's for a good reason. Parsing invalid data can be a mess. XHTML kind of gets away with it because it's only (or at least, it started as) a presentation technology. As soon as you try to extract meaning out of it, it opens up a big can of worms that nobody quite figured out yet. I mean - Google had to hire thousands of PhDs and run hundreds of thousands (if not millions) of computers to do it and get something meaningful out of it. There are some exceptions that may work in your favor. If the JSON file is correctly structured but the data has invalid references, then you may try to develop your own heuristics to figure out the correct references. You just parse the JSON structures as you need, and tidy up all the data later with correct references. If the JSON file itself has invalid structures (missing quotes, missing brackets, something like this) then your luck may vary. If the records are relatively regular you can also develop your own heuristics, making reasonable assumptions to recover data (by the way, that's similar to the rendering of buggy HTML data in today's browsers).

Carlos Ribeiro at Quora Visit the source

Was this solution helpful to you?

Other answers

https://github.com/garycourt/JSV This is a decently active project. It uses the JSON Schema specification http://tools.ietf.org/html/draft-zyp-json-schema-03 which I believe Gary Court himself wrote. This library is really awesome. You provide schemas that explain the format of a JSON document, check your data against the corresponding schema, and it provides a really detailed array of errors for the passed in data. The only bad thing is the current JSON specification does not contain support for Date data types. But you can very easily specify that the field is a string that will only validate when it passes a regular expression test that only accepts strings that can be parsed into a Date object. A better but slightly more complicated solution is to create your own schema environment. JSV allows you to create environments to work in. The default environment is the latest JSON specification. But you can just copy that and implement your own Date data type. [Edit] I've since come to find that JSV is super slow. I've found that the best balance of speed and features is amanda https://github.com/Baggz/Amanda which you can install with jam install amanda But there are other options: JSON Schema https://github.com/kriszyp/json-schema Schema https://github.com/akidee/schema.js http://cosmicrealms.com/blog/2012/01/13/benchmark-of-node-dot-js-json-validation-modules/

John Fawcett

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.