From the course: JSON Essential Training

Build a basic JSON structure - JavaScript Tutorial

From the course: JSON Essential Training

Build a basic JSON structure

- [Instructor] JSON is just text. So you can write and save JSON in any plain text editor. However, a code editor like. Visual Studio Code can be really helpful when you're working with JSON, because it includes tools that can point out some errors as you type, and it can also try to help you prevent making others. I'm in the highestFeatures.json file. And I want to create a JSON structure that stores the highest point, highest volcano and highest glacier on earth. Each of these needs a key for the label in addition to the value. So I need to use an object structure and object structure starts and ends with curly braces. So I'll start a new one and notice that when I type the opening curly brace, Visual Studio Code automatically adds the closing one. I love this because it makes it harder for me to forget, to close my braces at the end. Another great feature, if I press enter immediately, while my insertion point is still between the opening and closing braces. The editor moves the closing brace down two lines to the start of its own line. And on the line between the braces, it indents for me, indentation makes code much easier to read, especially when you're working with a more complex nested structure. So having that done for me, saves some work. First, I want to add the highest point. My label is point in double quotes. So I'll add that as the key, then I need a colon. And then the value, which is Mount Everest, the editor color code to the key and value different colors, which makes it easier to read the code and understand the meaning of each part at a glance. I have another key value pair after this one. So I'm going to type a comma and notice with that red squiggly line under the comma and the changes to the red names on the left and on the top that the editor is flagging an error. Now I haven't done anything wrong, but the editor is letting me know that if I stop now, I'd have a trailing comma, which JSON doesn't allow, but I'll press enter, and I'll start entering my next key, which is volcano. The value for volcano is Ojos Del Salado. And notice now that that error flag has disappeared because the comma I typed in the previous line is no longer the end of my JSON content. Now I'm going to need another comma. So I'm going to go through that error highlighting again. So we need a comment at the end of my second line, and then one more feature on a new line and that's glacier, and a colon. And the name is Khumbu Glacier. Now its important that I don't add a comma here. It's the last item in my JSON structure. So I can't include a comma after it. I already have my closing curly brace. So my structure is finished and I'll save it. Now that's an object structure, but what if I want to create a JSON array instead? Well, it's a bit different, but still pretty similar, I'll switch to the highest peaks.json file, an array starts and ends with square brackets. So I'll type that opening square bracket. The closing square bracket is auto completed and I'll press enter. when my pointers between the two of those to get an indented new line between them. Remember that I don't specify keys in an array, just values in order separated by commas. So once again, I'm starting with Mount Everest in quotes and then a comma and on the next line, it's K2 and a comma. And then finally Kangchenjunga, and no comma after this one, because it's the final one. And that's my JSON array.

Contents