A common use of JSON is to exchange data to/from a web server.
When sending data to a web server, the data has to be a string.
Convert a JavaScript object into a string with JSON.stringify()
.
Stringify a JavaScript Object
Imagine we have this object in JavaScript:
const obj = {name: "John", age: 30, city: "New York"};
Use the JavaScript function JSON.stringify()
to convert it into a string.
const myJSON = JSON.stringify(obj);
The result will be a string following the JSON notation.
myJSON
is now a string, and ready to be sent to a server:
Example
const obj = {name: "John", age: 30, city: "New York"};
const myJSON = JSON.stringify(obj);
Try it Yourself »
You will learn how to send JSON to a server in the next chapters.
Stringify a JavaScript Array
It is also possible to stringify JavaScript arrays:
Imagine we have this array in JavaScript:
const arr = ["John", "Peter", "Sally", "Jane"];
Use the JavaScript function JSON.stringify()
to convert it into a string.
const myJSON = JSON.stringify(arr);
The result will be a string following the JSON notation.
myJSON
is now a string, and ready to be sent to a server:
Example
const arr = ["John", "Peter", "Sally", "Jane"];
const myJSON = JSON.stringify(arr);
Try it Yourself »
You will learn how to send a JSON string to a server in the next chapters.
Storing Data
When storing data, the data has to be a certain format, and regardless of where you choose to store it, text is always one of the legal formats.
JSON makes it possible to store JavaScript objects as text.
Example
Storing data in local storage
// Storing data:
const myObj = {name: "John", age: 31, city: "New York"};
const myJSON = JSON.stringify(myObj);
localStorage.setItem("testJSON", myJSON);
// Retrieving data:
let text = localStorage.getItem("testJSON");
let obj = JSON.parse(text);
document.getElementById("demo").innerHTML = obj.name;
Try it Yourself »
Exceptions
Stringify Dates
In JSON, date objects are not allowed. The JSON.stringify()
function will convert any dates into strings.
Example
const obj = {name: "John", today: new Date(), city : "New York"};
const myJSON = JSON.stringify(obj);
Try it Yourself »
You can convert the string back into a date object at the receiver.
Stringify Functions
In JSON, functions are not allowed as object values.
The JSON.stringify()
function will remove any functions from a JavaScript object, both the key and the value:
Example
const obj = {name: "John", age: function () {return 30;}, city: "New York"};
const myJSON = JSON.stringify(obj);
Try it Yourself »
This can be omitted if you convert your functions into strings before running the JSON.stringify()
function.
Example
const obj = {name: "John", age: function () {return 30;}, city: "New York"};
obj.age = obj.age.toString();
const myJSON = JSON.stringify(obj);
Try it Yourself »
If you send functions using JSON, the functions will lose their scope, and the receiver would have to use eval() to convert them back into functions.
W3schools Pathfinder
Track your progress - it's free!
FAQs
Their examples are outdated, insecure, and don't really explain why things work in a certain way. Their tutorials are optimized for copy & paste, and God knows how many security bugs in the wild are due to somebody copying W3Schools.
Is W3Schools enough? ›
So,for starters and some easy fast crossreference,it is very good place. But once you get into some more advanced stuff,w3school will be limit with info and you will need to search elsewhere, like stackoverflow or even better,the language documentation itself. If it helps you then use it.
Is W3Schools certification legit? ›
W3Schools is Trusted by Top Companies
Our certificates are recognized and valued by companies looking to employ skilled developers.
Is W3Schools bootcamp worth it? ›
The W3C Bootcamp is an excellent program that far exceeded my expectations. Its contemporary syllabus, engaging learning environment, and practical approach make it an invaluable resource for anyone seeking to understand web development.
Which is better, Codecademy or W3Schools? ›
Of the two, Codecademy has a simpler structure. You can easily find the topic you want and start learning. Its fixed pricing plans are also easier to understand. In addition, the biggest issue with W3Schools is the ads that keep popping up as you learn.
Who is behind W3Schools? ›
It is run by Refsnes Data in Norway. It has an online text editor called TryIt Editor, and readers can edit examples and run the code in a test environment.
Can I get a job with W3Schools certificate? ›
Becoming certified by W3Schools is great proof to show to employers and makes your resume stand out among other candidates.
Is SoloLearn better than W3Schools? ›
Both. Both! W3 Schools is ideal for web development, and web development alone. SoloLearn on the other hand, teaches web development languages like HTML, JavaScript, and CSS, but offers so much more general purpose languages like C++ and Java.
How does w3school make money? ›
Through ads : through ads alone they will earn alot in w3schools. They would earn nearly 1–2lacks per day in ads alone. Through donation:. Many people who completed their courses in w3schools, after few years , they would donate some money for the growth of the site .
Is W3Schools enough for SQL? ›
Basically the title. Not only Select Star SQL misses many basic SQL operations, but its explanations are both unnecessarily complicated and miss lots of details. On top of that you spend half of your time reading about the executions in the US.
The largest web developer site on the internet.
- 3 billion pages displayed each year.
- 70 million visitors each month.
Is W3Schools completely free? ›
Both My Learning and W3Schools Public Profile are completely free of charge, however there are available upgrade plans that can enhance your learning experience. Where can I find My Learning? You can access your My Learning profile through the upper right corner on any W3Schools page.
Is it better to go to a coding bootcamp or college? ›
You can gain some great skills from a bootcamp, but a degree will help you understand the theory and maths behind the code.” “Coding bootcamps teach you how to code in the real world, but they don't make you an engineer.
How long does W3Schools take? ›
W3Schools is a tech training provider offering a 24-week, part-time, live online, Web Development Bootcamp.
How difficult is a coding bootcamp? ›
Coding bootcamps are hard because they are an intensive learning experience. Bootcamps fill an important educational gap between learning on your own and a traditional college program. Any bootcamp is hard, but some are harder than others because bootcamps vary in level, length, intensity, curriculum, and cost.
Is W3Schools good for kids? ›
Who can use W3Schools? W3Schools is accessible to everyone, from beginners to more experienced developers. It serves as a valuable resource for both groups, often used as a reference tool. It is particularly suitable for middle and high school students, aligning well with their educational needs and curriculums.
Is W3Schools a good place to learn SQL? ›
"Perfect e-learning course for beginners. The training is very well structured together with quizes after almost every lesson. Great job!
Are there any websites like W3Schools? ›
stack overflow is the largest, most trusted online community for developers to learn, share their programming knowledge, and build their careers. learn to code in python, c/c++, java, and other popular programming languages with our easy to follow tutorials, examples, online compiler and references.