Is there a tutorial that explains how to use RESTful APIs with JavaScript. Responses usually come in JSON-format, and I still don’t know how to handle them. I know jquery has something called JSON.parse(), which I don’t fully understand
Please tell me if there
Thank you!
Yes, this is a hard subject. I’d recommend checking out some youtube videos.
I know jquery has something called JSON.parse()
That’s actually just standard JavaScript. JSON is a data format that isn’t the same as a JS object but it is very compatible with it. Because the formatting in JSON is a little different, JSON comes in as a string version of the JSON object. JS has helper function to turn that string of encoded JSON into a true JS object, JSON.parse. It also has JSON.stringify, which does the opposite.
const obj1 = { prop1: 123, prop2: false };
console.log(typeof obj1);
// object
console.log(obj1);
// {prop1: 123, prop2: false}
const myJSONString = JSON.stringify(obj1);
console.log(typeof myJSONString);
// string
console.log(myJSONString);
// {"prop1":123,"prop2":false}
const obj2 = JSON.parse(myJSONString);
console.log(typeof obj2);
// object
console.log(obj2);
// // {prop1: 123, prop2: false}
You can think of JSON (at least the data it can represent) as a subset of JS objects. All JSON can be converted to JS objects, but not all JS objects can be converted to JSON objects - JSON can’t hold functions or Symbols or circular references, etc. Just objects, arrays, numbers, strings, and boolean - and any valid combination of those.
But yeah, it’s all pretty confusing. I think you just learn it by doing it. And to be honest, I didn’t really begin to understand APIs until I built a few of my own. But just keep working at it.
Thanks for that. My problem is how to use that with an API
This one for example:
Right. I would just look at some videos. Sure, I could write out 17 pages of explanation, but it is already being explained well elsewhere, so why bother?
Yes, things like jQuery and Axios are often used to retrieve API data, but I think starting out with the plain JS fetch is a good idea - once you understand that, the others will make sense.
I would look at videos like these and just work through them. Once you get the idea, you can look at dummy api data sites like this and just do it until it makes sense.
But again, I don’t think it will fully make sense until you work on the other side and build an API. But that’s OK, you can still get a good functional understanding.