The goal of this article is to clarify the differences between JavaScript Objects and JavaScript Object Notation(JSON). As beginners, while working with JSON data it is quite possible to confuse it with JavaScript Objects. While JSON cuts across different programming languages, JavaScript Objects are only peculiar to JavaScript.
Because of the similarities between JavaScript Objects and JSON, it is possible to use a javascript program to convert JSON data into native JavaScript Objects and vice versa.
JavaScript Object Notation(JSON) is a lightweight format for storing and transporting data. It is often used when data is sent from a server to a web page, it is also self-describing and easy to understand.
Douglas Crockford originally specified the JSON format in the early 2000s.
Since the JSON format is text only, it can easily be sent to and from a server, and used as a data format by any programming language.
A subset of javascript, JSON is virtually supported by all modern programming languages in one form or another.
The structure of JSON is based on two basic structures,
A collection of key / value pairs.
An ordered list of values.
Also, there are six data types allowed in JSON, and they include:
JavaScript Objects are used to store a collection of data. All JavaScript values, except primitive data types(Number, String, Boolean, null, undefined and symbol), are objects.
JavaScript Objects are a bit more complex in the sense that, they may contain a combination of primitive data types. They are contained in curly braces {…}, and can be created by object literals, object constructor syntax, constructors and prototypes.
In it’s basic form, javaScript Objects are key:value pairs, whereby a key is called a string, and a value can be called anything.
{
"name":"Ben Halp",
"age":35,
"car": null,
"address":{
"street": "55, cathedral road",
"city": "Boston",
"cop" : true
},
"hobbies":["reading", "golfing"]
}
From the above example, the name is set as string which has a double quote for both the key and the value, age is set as number, the address is included in an object, in the address object, the key cop is set as a boolean, car is null and hobbies is set as an array.
{
"name":"Ben Halp",
"age":35,
"car": null,
"address":{
"street": "55, cathedral road",
"city": "Boston",
"cop" : true //Boolean
},
"hobbies":["reading", "golfing"] //Array
}
The above code in the data.json file will give us an error because of the comment in the code.
It is also worthy to note that JSON is supported by more than 50 languages including JavaScript, Perl, Java, Python, Ruby, php, C.
Functions or methods are not allowed in JSON as they only support data format.
let person = {
name : "Erik Wolfgang",
location : "Europe",
established : "1945"
}
This makes it easier to differentiate javaScript Objects from JSON.
let member = {
name: "dave",
"amount": function() {
return 300000;
},
"city": "bristol"
};
From the above code, amount is a method of the member object, which contains data that is stored in the object property.
let data = { name:"Viktor", age:40, haircolor:"pink"}
let x = data;
x.age = 10; // This will change both x.age and data.age
let first = new Object(); // A new Object object
let second = new String(); // A new String object
let third = new Number(); // A new Number object
let fourth = new Boolean(); // A new Boolean object
let fifth = new Array(); // A new Array object
let sixth = new RegExp(); // A new RegExp object
let seventh = new Function(); // A new Function object
let eight = new Date(); // A new Date object
It is better to use primitive data types (string, number, boolean, null, undefined, symbol), as they are faster and not complex.
// Bracket Notation
let person = {
name : "Kudvenkat",
location : "India",
age : 95
}
console.log(person['name']); // Output : Kudvenkat
//Dot Notation
let person = {
name : "Kudvenkat",
location : "India",
age : 95
}
console.log(person.name); // Output : Kudvenkat
JSON can be converted to a javascript Object by using the JSON.parse() function. It is worthy to note that, if the data retrieved either from a file or a server is not in a JSON format, an error will occur.
// JSON data in a javaScript variable
let data = '{"name": "Andre", "age": 19, "country": "Venezuela"}';
// Converting JSON string into a javaScript object
let obj = JSON.parse(data);
// Get Data From javaScript Object
alert(obj.name); // Output: Andre
alert(obj.age); // Output: 19
alert(obj.country); // Output: Venezuela
From the example above, we were able to use the JSON.parse() function to convert a JSON string into a javascript Object, and also access the values of each string using the dot notation we spoke about earlier.
javascript Object can also be converted into JSON using the JSON.stringify() method.
// javaScript Object in a javaScript variable
let data = { name: "Perpetual", age: 26, city: "Lagos" };
// Converting javaScript Object into a JSON string
let obj = JSON.stringify(data);
// Display Data From a JSON string
alert(obj); // Output: {"name": "Perpetual","age": 26,"city": "Lagos"}
obj is now a string. Also JSON.stringify() is supported by all major browsers.
In JavaScript, almost everything is an object. So it is important for one to get very familiar with them. Additionally, Objects gives us the ability to read our code more clearly and also helps with better data representation.
With the help of this article, I believe you should be able to identify the major differences between JavaScript Object and JSON at first glance.
#javascript #json #web-development