Difference between JSON and JavaScript object

You can define a JavaScript object such as:

javascript
let erik = {
    age: 30,
    city: 'Barcelona'
}

Here is the same object in JSON:

JSON
{
    "age": 30,
    "city": "Barcelona"
}

JSON is a type of format to describe objects, compatible with different coding languages. The most noticeable difference between a JSON representation and a JavaScript object is the use of double-quotes.

Generally, JSON is used to represent server responses.

You can transform the JSON into a JavaScript object, using the JavaScript API.

Example:

javascript
let erik = JSON.parse(`{
    "age": 30,
    "city": "Barcelona"
}`);

console.log(erik);
// {age: 30, city: "Barcelona"}

And you can do the opposite as well:

javascript
let erik = {
    age: 30,
    city: 'Barcelona'
}

let json = JSON.stringify(erik);

console.log(json);
// {"age": 30, "city": "Barcelona"}

Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.