GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • JS Tutorial
  • Web Tutorial
  • A to Z Guide
  • Projects
  • OOP
  • DOM
  • Set
  • Map
  • Math
  • Number
  • Boolean
  • Exercise
  • JSON.parse()
  • JSON.stringify()

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — JavaScript JSON Objects - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • JS Tutorial
  • Web Tutorial
  • A to Z Guide
  • Projects
  • OOP
  • DOM
  • Set
  • Map
  • Math
  • Number
  • Boolean
  • Exercise

JavaScript JSON Objects

Last Updated : 26 Oct, 2026

JSON (JavaScript Object Notation) is a lightweight text format used to store and exchange data between applications.

frame_1000002048
  • Data Exchange: Transfers data between clients, servers, and applications.
  • JavaScript Integration: Converts JSON strings into JavaScript values using JSON.parse() .
  • Data Serialization: Converts JavaScript values into JSON strings using JSON.stringify().

Creating a JavaScript Object

JavaScript objects store data as key-value pairs. Although JSON uses a similar structure, a JavaScript object is not the same as JSON text.

JavaScript
const person = {
  "name": "Ben",
  "age": 30,
  "city": "New York"
};

console.log(person);

Output
{ name: 'Ben', age: 30, city: 'New York' }
  • {} defines the object.
  • "name", "age", and "city" are property names.
  • "Ben", 30, and "New York" are the corresponding values.
  • : separates property names and values, while , separates key-value pairs.

In JSON text, property names must use double quotes, strings must use double quotes, and functions and undefined values are not supported

Accessing JavaScript Object Properties

Object properties can be accessed using dot notation or bracket notation.

JavaScript
const myOrder = {
  name: "Earbuds",
  cost: 799,
  warranty: "1 year"
};

console.log(myOrder.name);
console.log(myOrder["cost"]);
console.log(myOrder["warranty"]);

Output
Earbuds
799
1 year
  • Dot notation: Access a property using object.property.
  • Bracket notation: Access a property using object["property"]. This is useful when property names contain spaces or are stored in variables.

Looping Through JavaScript Object Properties

The for...in loop iterates over an object's enumerable string-keyed properties. Bracket notation can be used to access each property's value.

JavaScript
const myOrder = {
  name: "Earbuds",
  cost: 799,
  warranty: "1 year"
};

for (const key in myOrder) {
  console.log(key, myOrder[key]);
}

Output
name Earbuds
cost 799
warranty 1 year

To iterate only over an object's own enumerable properties, use Object.keys(), Object.values(), or Object.entries(), depending on whether you need the keys, values, or both.

Converting a JSON String to a JavaScript Object

The JSON.parse() method converts a valid JSON string into a JavaScript value, such as an object or array.

JavaScript
const jsonString = '{"name": "GFG", "age": 30}';

const jsonObject = JSON.parse(jsonString);

console.log(jsonObject.name);
console.log(jsonObject.age);

Output
GFG
30

JSON.parse() throws a SyntaxError if the input contains invalid JSON. Use error handling when parsing JSON from external sources.

Converting a JavaScript Object to JSON

The JSON.stringify() method converts a JavaScript value into a JSON string. It is commonly used when sending data to APIs or storing data as text.

JavaScript
const person = {
  name: "Ben",
  age: 30,
  city: "New York"
};

const jsonString = JSON.stringify(person);

console.log(jsonString);

Output
{"name":"Ben","age":30,"city":"New York"}

JSON.stringify() serializes supported values into JSON text. Some JavaScript values, such as functions and undefined properties, are omitted from objects during serialization.

Comment

Explore

    JavaScript Basics
    • Introduction3 min read
    • Variables5 min read
    • Operators4 min read
    • Control Statements4 min read
    Array & String
    • Arrays6 min read
    • Array Methods4 min read
    • String5 min read
    • String Methods8 min read
    Function & Object
    • Functions4 min read
    • Function Expression3 min read
    • Function Overloading4 min read
    • Objects4 min read
    • Constructors4 min read
    OOP
    • OOP5 min read
    • Classes & Objects4 min read
    • Access Modifiers5 min read
    • Constructor4 min read
    Asynchronous JavaScript
    • Asynchronous2 min read
    • Callbacks4 min read
    • Promises4 min read
    • Event Loop3 min read
    • Async Await Fuction3 min read
    Exception Handling
    • Error and Exceptional Handling5 min read
    • Errors Throw & Try to Catch2 min read
    • Custom Errors2 min read
    • TypeError Invalid Array.prototype.sort argument1 min read
    DOM
    • DOM8 min read
    • DOM Elements3 min read
    • Custom Events4 min read
    • Addeventlistener5 min read
    Advanced Topics
    • Closure3 min read
    • Hoisting5 min read
    • Scope4 min read
    • Higher Order Functions7 min read
    • Debugging3 min read
    Courses
    • Placement 360 Course2 min read
    • MERN Full Stack Course2 min read
    • DSA and System Design Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.