JSON - Beginners Guide to learning JSON with JavaScript

JSON Tutorial For Beginners | What is JSON | Learning JSON with JavaScript

JSON Tutorial For Beginners | What is JSON | Learning JSON with JavaScript

JSON Tutorial For Beginners | What is JSON | Learning JSON with JavaScript

Explore JSON and how JavaScript Objects can be used to access data within JSON data format and output to your web page

Guide to learning how to use JavaScript Objects and JSON data. JSON is the most popular format for data exchange between applications. If you are interested in connected to a web API chances are its JSON formatted. Learn to use AJAX to connect and bring JSON data into your JavaScript!

This course shows you how to work with JSON formatted data, output content, loop JSON data, Parse JSON and a whole lot more.

JSON (JavaScript Object Notation) is a syntax for data. JSON is easier to use than XML and human readable. Most modern web APIs output data in JSON formats. It's a lightweight data interchange format that is quickly becoming the default format for data exchange on internet today! JSON is lightweight, language independent and easy to read and write. JSON is better than XML and more popular!

Within the lessons of this course we will explore

  • JavaScript data types used to hold variables and how they work
  • JSON and how to write JSON data
  • How to add values into a JSON object
  • Accessing JSON data and bringing it into JavaScript
  • JavaScript JSON parse and stringify methods
  • Adding JSON to local storage
  • Retrieving back data within JSON formats, updating and working with JSON
  • Connecting to a web API using fetch
  • Retrieving JSON data from a web API and outputting the results into your web page
  • Iterating threw multiple results from an API
  • Google Spreadsheet data as JSON and how to practice retrieving data
  • All of the source code and resources are in included
  • Explore different methods of working with the JSON data stringify and parsing
  • How JavaScript objects can use Arrays to hold multiple items
  • How JavaScript arrays work and store data

Thanks for reading

If you liked this post, please do share/like it with all of your programming buddies!

Follow us on Facebook | Twitter

Further reading about JavaScript and JSON

The Complete JavaScript Course 2019: Build Real Projects!

JavaScript Programming Tutorial | Full JavaScript Course for Beginners 2019

The complete beginner’s guide to JSON

The Complete Guide to JSON Web Tokens

JWT Fundamentals for Beginners

Best JavaScript Frameworks, Libraries and Tools to Use in 2019

New ES2019 Features Every JavaScript Developer Should Know

How to use JSON.stringify() and JSON.parse() in JavaScript

How to use JSON.stringify() and JSON.parse() in JavaScript

JSON.stringify() and JSON.parse() are useful tools for handling JSON-formatted content

There are two useful methods to deal with JSON-formatted content: JSON.stringify() & JSON.parse(), which are useful to learn as a pair.

JSON.parse() takes a JSON string and then transforms it into a JavaScript object.

const myObject = {
  dog: "🐕",
  cat: "🐈",
  koala: "🐨",
  count: 3
};

console.log(JSON.stringify(myObject));
// result: {"dog":"🐕","cat":"🐈","koala":"🐨","count":3}

console.log(JSON.parse(JSON.stringify(myObject)));
// result: Object {dog: "🐕", cat: "🐈", koala: "🐨", count: 3}

JSON.stringify() can take arguments, a replacer function and a string or number to use as a “space” in the returned string.

That means the replacer argument can be used to filter out values if you return undefined, as shown in the following example:

const user = {
  id: 101010,
  name: "Derek",
  email: "[email protected]"
};

function replacer(key, value) {
  if (typeof value === "number") {
    return undefined;
  }
  if (key === "email") {
    return "Removed for privacy";
  }
  return value;
}

console.log(JSON.stringify(user));
// result: {"id":101010,"name":"Derek","email":"[email protected]"}

console.log(JSON.stringify(user, replacer));
// {"name":"Derek","email":"Removed for privacy"}

console.log(JSON.stringify(user, null, "^_^ "));
// result: {
// ^_^ "id": 101010,
// ^_^ "name": "Derek",
// ^_^ "email": "[email protected]"
// }

console.log(JSON.parse(JSON.stringify(user)));
// result: Object {id: 101010, name: "Derek", email: "[email protected]"}

How do you spell stringify backwards? JSON.parse() (Photo by clement fusil on Unsplash)

JavaScript Objects vs JSON: What's the Difference?

JavaScript Objects vs JSON: What's the Difference?

In this article is to clarify the differences between JavaScript Objects and JavaScript Object Notation(JSON).

Introduction

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.

So, What is JSON?

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:

What are javaScript Objects?

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.

Features / Characteristics / Syntax of JSON
  • In JSON, the six data types which are supported will take their various forms.
{ 
  "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.

  • In JSON, it is strictly forbidden to make use of comments inside the file.
{ 
  "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.

Features / Characteristics / Syntax of javaScript Objects
  • Keys are not needed to be covered in double quotes and only the values are required to be covered in double quotes.
let person = {
    name : "Erik Wolfgang",
    location : "Europe",
    established : "1945"
}

This makes it easier to differentiate javaScript Objects from JSON.

  • Also, a javascript Object may also contain a function as one of it's members.
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.

  • javaScript Objects are mutable in nature. That is, they are objects whose state can be modified after they have been created.
let data = {  name:"Viktor", age:40, haircolor:"pink"}

let x = data;
x.age = 10;  // This will change both x.age and data.age
  • javascript Objects has it's own object version of primitive data types.
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.

  • The members of a javascript Objects can be accessed in two ways: The Bracket Notation and The Dot Notation.
// 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
Conversion of JSON Into javaScript Object

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.

Conversion of javaScript Object Into JSON

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.

Conclusion

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.