views:

309

answers:

3

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!

A: 

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

rogeriopvl
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
Kramer
i would use yml instead of json if its a config file. json is mainly for passing info between javascript and server
ErsatzRyan
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.
rogeriopvl
A: 

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

Andy Waite
+3  A: 

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.

pib
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): http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/289855
Paul Marshall