Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

I still don't know how to create and edit JSON files

I've gone through the Node.js lessons, and am about to finish the Express Basics lessons as well. I've yet to come across anything that actually teaches me how to create or manipulate a JSON file of my own. I don't understand why I'm learning about Jade and templates, but this information is basically unavailable. This seems like essential knowledge, and I don't see how a Full-Stack Javascript course could be considered complete without it. Am I missing something?

1 Answer

Have you watched the AJAX Basics Course? Dave McFarland explains how you can get data from a JSON file. I highly recommend you watch this course if you haven't. Hopefully you'll get a better understanding of what JSON is and what you can do with it. Here is an introduction to JSON. :)

This doesn't cover anything. I still don't know how to actually store data with JSON, which is the point of JSON. This only familiarizes me with the basic array-object structure used, and how to parse data from a pre-existing file. There is no information on how to apply this to my own code, other than pulling information to my own site/app from other sources.

I wouldn't say it doesn't cover anything. Remember that JSON stands for JavaScript Object Notation, so it's made of JavaScript objects. You can either create a file with a javascript object or assign an object to a variable. Example:

// Variable example
$variable = {
    "name": "John",
    "lastName": "Doe"
}

// To access the information inside $variable, just do the following
$variable.name; // John
$variable.lastName; // Doe

// JSON file example
{
   "name": "John",
    "lastName": "Doe"
}

// To get the data from the JSON file I prefer using jQuery. jQuery has a shorthand method called $.getJSON
$.getJSON("jsonFile.json", function(data) {
    var items = [];
    // In the code below, key is "name" and "lastName". val is "John" and "Doe". So the array will contain these items:
    // name John
    // lastName Doe
    $.each( data, function( key, val ) {
        items.push(key + " " + val);
    });
});

Here is more information about JSON. Here is more information about $.each jquery loop.