</> Code Corner

JavaScript · JS Functions & Objects · Lesson 43

JS JSON

JSON.parse and JSON.stringify.

JSON (JavaScript Object Notation) is a lightweight format for storing and transporting data, often used when data is sent from a server to a web page.

Example

<p id="demo"></p>
<script>
const text = '{"employees":[{"firstName":"John","lastName":"Doe"},{"firstName":"Anna","lastName":"Smith"}]}';
const obj = JSON.parse(text);
const back = JSON.stringify({ name: "Ada", age: 36, skills: ["math", "code"] });
document.getElementById("demo").innerHTML =
  obj.employees[1].firstName + " " + obj.employees[1].lastName + "<br>" + back;
</script>
  • Data is in name/value pairs, separated by commas
  • Curly braces hold objects; square brackets hold arrays
  • Keys must be strings in double quotes
  • Values can be strings, numbers, objects, arrays, booleans or null (no functions or dates)

Keep reading “JS JSON”

Sign up to read the full lesson, try every example in the live editor, and take the 5-question quiz.

Chat with us 👋