W3Schools.com (2024)

A common use of JSON is to exchange data to/from a web server.

When receiving data from a web server, the data is always a string.

Parse the data with JSON.parse(), and the data becomes a JavaScript object.

Sending Data

If you have data stored in a JavaScript object, you can convert the object into JSON, and send it to a server:

Example

const myObj = {name: "John", age: 31, city: "New York"};
const myJSON = JSON.stringify(myObj);
window.location = "demo_json.php?x=" + myJSON;

Try it Yourself »

Receiving Data

If you receive data in JSON format, you can easily convert it into a JavaScript object:

Example

const myJSON = '{"name":"John", "age":31, "city":"New York"}';
const myObj = JSON.parse(myJSON);
document.getElementById("demo").innerHTML = myObj.name;

Try it Yourself »

JSON From a Server

You can request JSON from the server by using an AJAX request

As long as the response from the server is written in JSON format, you can parse the string into a JavaScript object.

Example

Use the XMLHttpRequest to get data from the server:

const xmlhttp = new XMLHttpRequest();
xmlhttp.onload = function() {
const myObj = JSON.parse(this.responseText);
document.getElementById("demo").innerHTML = myObj.name;
};
xmlhttp.open("GET", "json_demo.txt");
xmlhttp.send();

Try it Yourself »

Take a look at json_demo.txt

Array as JSON

When using the JSON.parse() on JSON derived from an array, the method will return a JavaScript array, instead of a JavaScript object.

Example

JSON returned from a server as an array:

const xmlhttp = new XMLHttpRequest();
xmlhttp.onload = function() {
const myArr = JSON.parse(this.responseText);
document.getElementById("demo").innerHTML = myArr[0];
}
}
xmlhttp.open("GET", "json_demo_array.txt", true);
xmlhttp.send();

Try it Yourself »

Take a look at json_demo_array.txt


W3schools Pathfinder

Track your progress - it's free!

W3Schools.com (2024)
Top Articles
15 Best Canadian Blue Chip Stocks for March 2024
1 Stock I Wouldn't Touch With a 10-Foot Pole | The Motley Fool
Jazmen Jafar Linkedin
Nco Leadership Center Of Excellence
Kristine Leahy Spouse
Autobell Car Wash Hickory Reviews
Aces Fmc Charting
Achivr Visb Verizon
Morgan Wallen Pnc Park Seating Chart
Best Pawn Shops Near Me
Simple Steamed Purple Sweet Potatoes
zopiclon | Apotheek.nl
Meritas Health Patient Portal
Stihl Km 131 R Parts Diagram
SXSW Film & TV Alumni Releases – July & August 2024
Vistatech Quadcopter Drone With Camera Reviews
St Maries Idaho Craigslist
Ibukunore
SF bay area cars & trucks "chevrolet 50" - craigslist
Football - 2024/2025 Women’s Super League: Preview, schedule and how to watch
Sodium azide 1% in aqueous solution
THE FINALS Best Settings and Options Guide
Roane County Arrests Today
Dark Entreaty Ffxiv
Boise Craigslist Cars And Trucks - By Owner
Harrison County Wv Arrests This Week
Dr Seuss Star Bellied Sneetches Pdf
Buhl Park Summer Concert Series 2023 Schedule
Annapolis Md Craigslist
Noaa Marine Forecast Florida By Zone
United E Gift Card
Pokemmo Level Caps
Rvtrader Com Florida
Why Gas Prices Are So High (Published 2022)
Cl Bellingham
Vivek Flowers Chantilly
NHL training camps open with Swayman's status with the Bruins among the many questions
Davis Fire Friday live updates: Community meeting set for 7 p.m. with Lombardo
The best specialist spirits store | Spirituosengalerie Stuttgart
Powerboat P1 Unveils 2024 P1 Offshore And Class 1 Race Calendar
Juiced Banned Ad
National Weather Service Richmond Va
The Horn Of Plenty Figgerits
Copd Active Learning Template
Random Animal Hybrid Generator Wheel
Tacos Diego Hugoton Ks
Wpne Tv Schedule
Waco.craigslist
Premiumbukkake Tour
Rheumatoid Arthritis Statpearls
Walmart Front Door Wreaths
Tanger Outlets Sevierville Directory Map
Latest Posts
Article information

Author: Jamar Nader

Last Updated:

Views: 5529

Rating: 4.4 / 5 (75 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Jamar Nader

Birthday: 1995-02-28

Address: Apt. 536 6162 Reichel Greens, Port Zackaryside, CT 22682-9804

Phone: +9958384818317

Job: IT Representative

Hobby: Scrapbooking, Hiking, Hunting, Kite flying, Blacksmithing, Video gaming, Foraging

Introduction: My name is Jamar Nader, I am a fine, shiny, colorful, bright, nice, perfect, curious person who loves writing and wants to share my knowledge and understanding with you.