Code has been added to clipboard!

JavaScript JSON Object: Discover Best Tips

Reading time 3 min
Published Aug 10, 2017
Updated Oct 10, 2019

Understanding how you can manipulate and convert your data is crucial in any project. One of the most common ways of dealing with this problem is using universal formats.

Coders commonly use JavaScript JSON because it's easier: JSON is based on JavaScript.

After reading this tutorial, you'll be able to parse JSON text into a JavaScript object and use it in your daily tasks. You'll also understand JSON syntax, which you might find similar to writing JavaScript objects and arrays.

JavaScript JSON: Main Tips

  • JSON is a format you can use to store and transport data.
  • Most popular use of JSON is sending data from servers to web pages.
  • It's a resource-friendly format mainly used for working with data.

What JavaScript JSON is

  • The name of JSON stands for JavaScript Object Notation.
  • JSON is not dependent on the coding language.

Syntax of JSON

To get a better understanding of this data format, we will view a few JSON format examples. Let's get a few basic rules right first:

  • Data is put in pairs of name/value, then separated by commas.
  • Curly braces are used to contain objects.
  • Square brackets are used to contain JSON arrays.

For example, to describe the object employee, we'd use the JSON syntax below:

Example
"employee":[
    {"firstName":"Bob", "lastName":"Dilon"}, 
    {"firstName":"Ben", "lastName":"Aflik"},
    {"firstName":"Natalie", "lastName":"Kidmun"},
]

Note: JSON syntax originated from JavaScript object notation, yet the JSON format is a text-only format. It can be defined/written in various programming languages.

The data used with JSON is inserted as pairs of name/value. The pair of name/value has a field name, a colon and a value:

Example
"name":"Bob"

The objects of JSON are written within curly brackets. They can hold various pairs of name/value:

Example
{"firstName":"Bob", "lastName":"Doe"}

JSON arrays are written within square brackets. You can also have JSON array of objects:

Example
"employee":[
    {"firstName":"Bob", "lastName":"Dilon"}, 
    {"firstName":"Ben", "lastName":"Aflik"},
    {"firstName":"Natalie", "lastName":"Kidmun"},
]

JSON Object

A popular way of using JSON is to work with data from the web server and then display it on a web page. We will demonstrate this using a JSON format example with a string as our input.

First, we produce a JavaScript string holding the JSON syntax:

Example
var userData = '{ "employee" : [' +
'{ "firstName":"Bob" , "lastName":"Dilon" },' +
'{ "firstName":"Ben" , "lastName":"Aflik" },' +
'{ "firstName":"Natalie" , "lastName":"Kidmun" } ]}';

Then, by using the integrated JavaScript JSON.parse() function, we change the string to an object of JavaScript:

Example
var ob = JSON.parse(txt);

The last step is to work with the new object of JavaScript on the page:

Example
document.getElementById("example").innerHTML =
ob.employee[1].firstName + " " + ob.employee[1].lastName;

JavaScript JSON: Summary

  • You can use JSON to store and transport data.
  • JSON syntax is the same as the code for producing objects in JavaScript.
  • JavaScript programs can easily convert JSON array of objects into native objects of JavaScript.
  • JSON isn't dependent on the coding language.
What Is JavaScript Used For?
Tutorial
Introduction
Output
Syntax
Comment
Commands
Operators
Comparison and Logical Operators
Data Types
Math.random()
Type Conversion
Function Definitions
Events
Objects
Object Properties
Prototype
Array
Sorting Arrays
Strings
Numbers
Number Format
Math Object
Onclick Event
Date
Date Formats
Scope
Regular Expressions
Reserved Words
Common Mistakes
Performance
Forms
Form Validation
Window: The Browser Object Model
Popup Boxes
Cookies
JSON
AJAX Introduction
AJAX Form
Automatic File Download
Functions
Array Methods
String Methods
Date Methods
Timing Events
Cheat Sheet
JavaScript in HTML
HTML DOM Methods
HTML DOM Changing HTML
HTML DOM Animation
HTML DOM EventListener
HTML DOM Navigation
HTML DOM NodeList
HTML DOM Element Nodes
Array Functions
Boolean
Calling a Function
Date Functions
Global Objects
Input Text
Operator
Statements
String Functions
Math
Math.random
Number
RegEx
alert
array.filter
array.length
array.map
array.reduce
array.push
array.sort
break and continue
className
confirm
decodeURIComponent
for
forEach
if
indexOf
innerHTML
location.reload
number.toString
onclick
onload
parseInt
prompt
replace
setAttribute
setInterval
setTimeout
slice
splice
string.includes
string.indexOf
string.split
style.display
submit
substr
substring
switch
test
throw, try and catch
toLowerCase
toUpperCase
use strict
while
window.history
window.location
window.navigator
window.screen