A few weeks ago, I began writing an article on
Net::HTTP for SitePoint Ruby. I usually like to write out my ideas, thoughts, and other outline-y things on a whiteboard. However, this time was different. Don’t get me wrong, I love a good whiteboarding session, but I needed something a little more flexible.
I realized that JSON is perfect for this.
.json files. Let’s look at an example:
As you can see, it’s structured, organized, and makes sense. But how do we go about thinking in this way?
I like to start with a
postData object, which contains some info about my post.
Then, I add another object, titled
outline. Inside this object, I add a simple outline of what I want.
Just by looking at this, you can understand what the post will probably look like. This also gives us the flexibility to add anything, anywhere, without deleting anything unneccessarily. You can also check this file into Git (at least I hope you are), and outline worry-free.
Notice that I used arrays to define lists of things. If you were to write this out, arrays would be bullet points. Also notice that I used nested arrays and key-value pairs. Remember that data structures in JSON can be nested.
I find that JSON is a great solution when it comes to writing out your thoughts. You can really write it any way you want.
This is awesome, but how can we automate this? Well, if you check out my blog’s Rakefile, I have some pretty sweet automation tasks on there. One of them is
rake draft. This creates a new draft in the
_drafts directory, and a new JSON file to go with it in the
_outlines directory (you can see the source of my blog here).
Here’s the source of that task:
When you run
rake draft it prompts you for a title, then generates the files for you.
JSON is great, but if you’re feeling adventurous like me, you might realize that Ruby has a very similar data structure called a hash. Also, these two data structures can convert to and from eachother:
Isn’t this awesome? I really don’t have a preference over the two. I find that JSON is more portable though. Here’s our JSON as a Ruby hash:
They look very similar. The main difference is that
=> is used instead of
:. You can use
:symbols in Ruby hashes.
Whether or not you found this interesting, I find this really helpful when writing, or just trying to organize my thoughts. If you have any questions, comments, or concerns, feel free to drop me a line on Twitter @JesseHerrick.