2

i am using ruby gems json_pure and when i get parsing errors i am not able to determine the line number where the error is occuring. i was expecting to find a validator written in ruby that would tell me the line number. what is the best ruby approach to finding the json errors quickly?

thanks!

4 Answers 4

3

You could try Kwalify: http://www.kuwata-lab.com/kwalify/ruby/users-guide.html

It's not just a JSON/YAML validator, but you give it a schema and it validates against that.

You could use that to verify that your config file is correct (as per your definition of "correct") as well as correct JSON (or YAML), and it will tell you what line number the error happened on, and a bit of context for the error as well.

I removed a ']' from a sample in their documentation and the error message it gave me was

ERROR: document12a.json:10:1 [/favorite] flow sequence is not closed by ']'.

It also does data binding class generation if you want. It seems like a pretty good configuration management/validation tool.

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

2 Comments

Kwalify is great at parsing JSON that's also YAML, but I've thrown perfectly valid JSON into Kwalify and gotten cryptic errors like "mapping key is expected" (I'm guessing it's a whitespace issue, either the newlines just before the open braces or inconsistent tabs/spaces for indentation). I'm currently using a home-rolled JSON validator modified from this Ruby Quiz answer (you need to add your own line-counting bit, but a minor tweak to the "ws" function will give you that): blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/289855
Paul, I ran into the "mapping key is expected" error too. I had better luck using Yajl to parse and Kwalify to validate.
1

You might find it easier to use http://www.jsonlint.com/ to check the JSON is valid - it will highlight any problematic lines.

Comments

1

It's and old question, but it's still an issue in current Ruby.

Default JSON parser in Ruby does not tell you the line number and/or column number the parse error occured. Just 'parse' error and that's it.

You could change the parser to Oj and use with MultiJson gem. Oj is great, it's very fast, and it will show the line and even the column number the parsing errors occured! Great.

Sample error for one-line very big JSON (170KB+), Oj will give column 102421.

.../adapters/oj.rb:15:in `load': unexpected character at line 1, column 102421 [parse.c:666] (MultiJson::ParseError)

Comments

-4

JSON is supposed to be sent over the network as a string, so there's actually only one line.

3 Comments

i use it as configuration of an application instead of xml. its about 950 lines long and when i get a parse error i have to go thru and either visually identify the error or start divide/conquer approach. i was hoping that there was something that would help me find the error quicker
i would use yml instead of json if its a config file. json is mainly for passing info between javascript and server
Like @ErsatzRyan said, you will be much better off with yaml. JSON is not the best choice for configuration files, actually ruby is know to poorly support JSON. YAML, on the other hand, is used in rails for config files.

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.