Learning Node.js Development
上QQ阅读APP看书,第一时间看更新

JSON

Now that you know how to parse command-line arguments using process.argv and yargs, you've solved the first piece to the puzzle for the notes application. Now, how do we get that unique input from the user? The second piece to the puzzle is to solve how we store this information.

When someone adds a new note, we want to save it somewhere, preferably on the filesystem. So the next time they try to fetch, remove, or read that note, they actually get the note back. To do this, we'll need to introduce something called JSON. If you're already familiar with JSON, you probably know it is super popular. It stands for JavaScript Object Notation, and it's a way to represent JavaScript arrays and objects using a string. Now, why would you ever want to do that?

Well, you might want to do that because strings are just text, and that's pretty much supported anywhere. I can save JSON to a text file, and then I can read it later, parse it back into a JavaScript array or object, and do something with it. This is exactly what we'll take a look at in this section.

To explore JSON and how it works, let's go ahead and make a new folder inside our project called playground.

Throughout the book, I'll create the playground folders and various projects, which store simple one-off files that aren't a part of the bigger application; they're just a way to explore a new feature or learn a new concept.

In the playground folder, we'll make a file called json.js, this is where we can explore how JSON works. To get started, let's make a very simple object.