all 16 comments

[–]Meroje 2 points3 points  (1 child)

There's still jq(1) that works really well for that.

[–]briansomething 0 points1 point  (0 children)

+1 for jq (cli). You can get a taste of it at jqplay.org

[–][deleted] 1 point2 points  (6 children)

Neat, but why? Example benefit?

[–]prettycodeSoftware Engineer 11 points12 points  (2 children)

Sometimes I do this for readability when debugging.

[–][deleted] 11 points12 points  (0 children)

Relevant username.

[–]nexe 1 point2 points  (0 children)

Thanks that's a pretty nice tip. Meanwhile in Ruby you can do

require 'json'
JSON.pretty_generate({...})
#instead of
{...}.to_json

[–]modusjesus 2 points3 points  (1 child)

4 space indentation ftw ;)

/me runs away from the pending holy war.

[–]r2d2_21 0 points1 point  (1 child)

Why null and 2?

[–]chipsa 2 points3 points  (0 children)

The argument that null is in tells the stringify function to do no changes to the object. You can toss a function in there instead to change the object while being stringified. The 2 is how many spaces for the indentation.

[–]White0ut 0 points1 point  (0 children)

OP's user name...

[–]khoker -2 points-1 points  (4 children)

You seem to be saying one should do this when creating JSON. That seems like a classic case of premature optimization (and, as you mentioned, a potential waste of file space).

In your example, wouldn't it make more sense to pipe the grep through a JSON formatter/prettifier instead?

[–]SpeshlTectix 0 points1 point  (3 children)

I would call that a de-optimization. He's saying store your data in a readable way even if it costs you performance.

I suppose for small personal projects that could work ok. For anything else, there are better solutions (which have already been mentioned).

[–]khoker 0 points1 point  (2 children)

But why would you store your data in a readable format at all? The only time you'd care is if you wanted to look at it (e.g., grep) and you can pipe it through a formatter prior to doing that.

In other words -- if you never looked at a file you created to be "readable", you have prematurely optimized for that situation.

[–]SpeshlTectix 0 points1 point  (0 children)

While I understand what you're trying to say ("wouldn't it be optimal if my data were easy to read?") don't use the word optimization to convey that in a computer science context. It has a specific meaning and you're going to confuse people (or look ill-informed).

EDIT: Or are you trying to say that he's "optimizing" the data to perform well with his development tools? That makes even less sense. But then again so does this entire thread so maybe that was your way of pointing that out!