Newbie Question Re: JSON

I’m just learning to work with AJAX, which attracted my attention to JSON. It looks really cool, but I don’t think I’m seeing the big picture yet.

For example, I do a lot of work with information related to animals. So imagine the following JSON file describing the lion (Panthera leo):

{
  "genusName": "Panthera",
  "speciesName": "leo",
  "home": {
    "biome": "savanna",
    "ecozone": "Afrotropic, Indo-Malaya",
  },
  "diet": [
    {
      "type": "vertebrates",
    },
    {
      "behavior": "social"
    }
  ],
  "ecology": {
    "type": "predator"
  }
}

How would that be an improvement over putting the same information in a database table? Should I simply think of JSON as an alternative database (i.e. you can choose between MySQL or JSON)? Or does JSON offer some sort of advantage over a database?

In summary, JSON looks cool, but I haven’t yet grasped exactly what it can do that I’m not already doing with my database.

Thanks!

An ajax call can’t retrieve your database directly - you have to extract the data from the database and then convert the data to a version ajax can read - normally either xml or json.

json is the easiest format for JavaScript to work with because that’s just the simplest way to define objects in native JavaScript.

1 Like

Oh, that makes sense. Thanks.

Wow, I think you answered TWO questions… I’ve started exploring the possibility of using AJAX to display dynamic data. Apparently, it can be done. But I guess JSON would be an alternative way of doing that, too.

Yep, JSON is just the format that the data is sent in with these ajax requests, JSON is a text string representing the object, and besides a couple of edge cases you can convert back and forth.

var object = {
  text: 'yep',
  number: 123
};
var json = JSON.stringify(object);
console.log(json);
var newObject = JSON.parse(json);
console.log(newObject);

So you JSON.parse the string returned from the server and then can work with the dynamic data in JavaScript.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.