0

Is this is a JavaScript Object or JSON?

var j = {
    "countries": {
        "country": [{
            "cname": "Japan",
            "capital": "Tokyo"                    
        },
        {
            "cname": "India",
            "capital": "Delhi"
        }]
    }
};

If it is a JSON, how do I make it a JavaScript object. If it is a JavaScript object, how do I make it JSON?

CuriousDev
  • 1,255
  • 1
  • 21
  • 44

3 Answers3

6

This is a JavaScript object. JSON is a data serialization format.

To be more precise - this is a variable assignment where you're assigning a JavaScript object literal to a variable named j. Please refer to the tag for more information about the difference.

Some bits that confuse people:

  • JSON is a way to transfer data from one language to another, it is loosely based on the JS object literal notation.
  • JSON is a text format, just like "var x = 3;" is a JS string inside js "null" is a JSON string inside JS. You can serialize/parse it using JSON.parse and JSON.stringify.
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
1

That is a Javascript object, specifically a object literal that is assigned to a variable.

The JSON form is the text that represent the object:

{
    "countries": {
        "country": [{
            "cname": "Japan",
            "capital": "Tokyo"                    
        },
        {
            "cname": "India",
            "capital": "Delhi"
        }]
    }
}

You can have the JSON as a string in the Javascript code, for example:

var json = '{"countries": {"country": [{"cname": "Japan","capital": "Tokyo"},{"cname": "India","capital": "Delhi"}]}}';

To turn a string containing JSON into a Javascript object, you would parse it. The JSON object is available in recent browsers, where you can use the parse method:

var j = JSON.parse(json);

To turn a Javascript object into JSON, you can use the stringify method:

var json = JSON.stringify(j);

The JSON format was constructed as a subset of Javascript syntax so that you could easily parse it using the eval method that was available in browsers at the time. As the eval method will execute the string as code, that is a potential opening for cross scripting attacks, so you should use a method that parses the string instead of evaluating it when possible.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • Ok so let me get this correct. If I were to use the code I posted originally, I would call it a JavaScript object. But if I remove the variable "var j =", it is valid JSON? – CuriousDev Sep 29 '14 at 10:47
  • @user1089173 that is correct. – Benjamin Gruenbaum Sep 29 '14 at 10:48
  • @user1089173: Yes, any JSON text can be used as Javascript code. The difference is mainly how you use it, not the syntax (except that the JSON format is a subset of Javascript syntax). – Guffa Sep 29 '14 at 10:48
  • can you elaborate - " The difference is mainly how you use it, not the syntax". Isn't the syntax also important. For eg: I can use counties without quotes if it were a JS object but not in JSON. – CuriousDev Sep 29 '14 at 10:50
  • @user1089173: What I mean is that it's the usage that determines if it is JSON or Javascript. If you remove quotation marks from JSON it doesn't become Javascript, it's just invalid JSON because it's used as text, not as code. – Guffa Sep 29 '14 at 10:53
0

If a file/string contains following text, it is JSON

{
    "countries": {
        "country": [{
            "cname": "Japan",
            "capital": "Tokyo"                    
        },
        {
            "cname": "India",
            "capital": "Delhi"
        }]
    }
}

If you execute following JavaScript, value of variable j will be a JavaScript object, which has a property countries.

var j = {
    "countries": {
        "country": [{
            "cname": "Japan",
            "capital": "Tokyo"                    
        },
        {
            "cname": "India",
            "capital": "Delhi"
        }]
    }
};

JSON is a data interchange format. It is language independent. It is based on JavaScript's object literal notation, but JSON is absolutely not JavaScript or a subset of it.


You can convert a JavaScript object to JSON using JSON.stringify(). You can convert a JSON object to JavaScript object using JSON.parse().

sampathsris
  • 21,564
  • 12
  • 71
  • 98