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

Why does the JSON method not work on Facebook?

I've been experimenting a bit on about JSON and here are the observations I made from Facebook:

  1. why are the URLs different for different persons?
  2. Don't they use javascript?
  3. If they do, then why does .json at the end of the id doesn't work like it does on treehouse?

Hope someone will answer this ( somewhat weird ) question of mine. Thank you !!!

2 Answers

JSON is just a method of formatting data so it looks like JavaScript. It's one of the most popular data formats out there, and can be written with any language on any platform (it's just a specially-formatted string, after all). Most of your questions deal with how a server is configured instead of JSON itself.

URLs are different for different people on Treehouse because Treehouse uses your username to distinguish you from other people, and since it's unique, they use it in the URL for your page. Facebook does the exact same thing. They assign each user a username (you can actually change it if you go into your profile settings), and use that in the URL for each user's page because it's a unique identifier.

Facebook uses JavaScript on the front-end. In fact, they wrote a really cool and hella popular framework for JavaScript called React. That being said, their backend is mostly in PHP and their own derivative of PHP called Hack.

When you want to get data from a website in code, you usually use their API, if they have one (API stands for "Application Programming Interface"). Treehouse only wants to offer data about users, so their API is really lightweight compared to lots of other places. For Treehouse, you just need to add ".json" onto the end of someone's URL, and it'll give you information about that person in JSON format. This isn't altogether uncommon, but as far as APIs go, it's a tad of an odd way to go about it (though I think Twitter does a similar thing for their users, but you have to do some standard setup beforehand). Facebook is really popular and has a lot of data they want to hand out, so just adding .json doesn't work too well for their use case. Instead, they offer what they call their Graph API. In other words, the user.json thing mostly just works on Treehouse, because that's how Treehouse set up their server. If you wanna get data from a website, you should look for their API. It's great you're applying the stuff you've learned here to other places, though!

Thanks for the effort, Michael Hulet.