1

I'm writing a program in Ruby that is parsing a fairly large JSON file. I'd like to be able to run a piece of Ruby code (in a testing environment) that parses this file, and see the information that has been extracted in a readable "pretty print" kind of way, for the sole purpose of testing.

So far I've just been testing things using irb in the terminal, but the output has no formatting whatsoever, so it's very difficult to figure out if things are working correctly. Is there a tool that makes JSON parsing a bit less painful?

user2901181
  • 343
  • 4
  • 17
  • Can't you use the json gem? – sawa Jun 22 '15 at 13:08
  • Your point is not clear. Do you want to parse JSON, or do you want to pretty print a complicated object (which has nothing to do with JSON)? – sawa Jun 22 '15 at 13:09
  • @sawa I'm writing a script that will pull info from the JSON file. I just want to know if there's some sort of testing environment I can use to run Ruby parsing code on the JSON file and then see the result of that, for the sole purpose of me seeing if it's working correctly. – user2901181 Jun 22 '15 at 13:13
  • What about using `jq` ? – hek2mgl Jun 22 '15 at 13:13
  • 1
    @hek2mgl Actually, this is exactly the kind of thing I was looking for! – user2901181 Jun 22 '15 at 13:24
  • please put the code you are trying to test, so that we can provide better support. – Nafaa Boutefer Jun 22 '15 at 13:27
  • `jq` is an awesome tool; I use it all the time. These days I combine it with `http` as an easier-to-remember `curl`. But what you're doing isn't really "testing", per se, rather Looking Manually At Output (LMAO). If you want to *test*, then actually test (Java version of LMAO http://stackoverflow.com/a/10859000/438992), otherwise it's just exploration. Exploration is *good*, but transitory. – Dave Newton Jun 22 '15 at 13:27
  • @DaveNewton Right, I have trouble sometimes articulating exactly what I'm trying to do, so apologies for misleading anyone. Thanks for info though. This will be very helpful for me. – user2901181 Jun 22 '15 at 13:37

3 Answers3

1

awesome_print is a very convenient gem for this purpose. And about the REPL, I suggest to use pry instead of irb.

Nafaa Boutefer
  • 2,169
  • 19
  • 26
0

I suppose you are using json from the stdlib?

Anyhow, you can test your code with minitest or rspec. I would not test for readability of your output, but rather test that given an input X, you get expected output Y.

zwippie
  • 15,050
  • 3
  • 39
  • 54
0

You can copy and then paste a JSON object string into a code cleaner like http://www.dirtymarkup.com/ which will re-format the code into a more readable format.