What Is JSON? | A Beginner’s Guide | MongoDB (2024)

Many people think of JSON (JavaScript Object Notation) as a language, but it’s really just a data interchange format that works natively with most modern languages, like Python, Java, PHP, and JavaScript. JSON is commonly used for transmitting data in web applications.

As the name implies, JSON is very similar to JavaScript objects in the way it’s written and stored. However, JSON has only properties and no methods.

In this introductory article, we will learn what JSON is, the syntax, JSON examples, and why we need JSON.

JSON explained

JSON is a text-based data exchange format and has a syntax similar to JavaScript objects.

You can use JSON when writing asynchronous web applications that exchange data from server to a web page and need very fast data access—for example, websites having dynamic content that needs to update based on user input, or suggestions based on user preferences.

What Is JSON? | A Beginner’s Guide | MongoDB (1)

JSON is a text-based data interchange format for passing data between client and server over a network.

Here is an example of data stored in JSON format:

{ "studentDetails": { "name" : "Joe", "age" : 16, "dept" : "computers", "hobbies" : ["dance", "books", "public speaking", "golf"], "isClassLeader" : false }}

Let’s explain the JSON syntax:

  • JSON objects are written within {curly} braces.

  • Each item is a key-value pair.

  • The keys and string type values are written within double quotes. Other data types—like integer and boolean—don’t need to be written in quotes.

  • Each item is separated from the next one using a comma (,). There is no comma after the last item.

  • Arrays inside JSON strings are written within [square] brackets.

  • Objects and arrays can be embedded within an object

When a formal schema is needed, it’s possible to create one following the JSON schema standards.

Unlike JavaScript objects, JSON cannot accept functions, date type, and undefined type. Dates can be converted to string in ISO format and stored.

You can create JSON objects inside an array, and arrays inside those JSON objects, as well:

{ "continentName": "North America", "area": 24.71, "countries": [ { "countryName" : "Mexico", "countryCode" : "+52", "location" : "south of north america", "languagesSpoken" : ["spanish", "maya", "nahuatl"] } ]}

This can get more complex as more information is added. But you can easily validate the JSON syntax using online JSON validators.

JavaScript natively supports switching between JavaScript objects and JSON string using the in-built global object: JSON. The JSON object uses the JSON.stringify() method to convert JS object to JSON string and JSON.parse() to convert JSON string to JS object.

What is JSON used for?

As we highlighted earlier, the most common use of JSON is to transmit data in web applications. Some typical uses of JSON are:

  • Transporting data between client and server in web applications—before JSON, data was transported using formats like XML. However, these have a lot of extra overheads, especially parsing and data mapping. JSON is light-weight and flexible, and all that is transferred from client to server and back is data! Also, the data is easy to read in name-value pairs.

  • Reducing development time of web applications—JavaScript (JS) is one of the essential languages used for web applications. JS stores all the data collected from a user—for example, form data—as objects. This data can be sent to the server (back-end) without much coding effort using JSON.

  • Faster availability of data—Most web applications nowadays are asynchronous, where data should be available faster and without a page refresh. Using JavaScript, applications can send JSON data quickly.

BSON—a binary version of JSON

Using native drivers, web applications can directly send data to MongoDB in JSON format. MongoDB internally stores data as BSON (Binary JSON), which is faster compared to JSON itself when it comes to querying the data. Also, BSON storage is more flexible and space-efficient. Using BSON at the back end also saves a lot of coding time for developers as there is no need for object mapping or conversions.

JSON examples

An important feature of JSON is that it’s very easy to fetch values, even from nested objects.

Consider the object defined in the previous example: to fetch values. We need to assign the object to a variable.

let exampleJsonObj = { "continentName": "North America", "area": 24.71, "countries": [ { "countryName" : "Mexico", "countryCode" : "+52", "location" : "south of north america", "languagesSpoken" : ["spanish", "maya", "nahuatl"] } ]}

Note: To try the below JSON examples, you can simply create a .html, and add the code inside <script> tags.

For example, you can get the “continentName” from our previous example, simply by giving the object name and the key:

document.write(exampleJsonObj.continentName);

This will print the answer North America on the page. To get the value of an array, you should specify the index. Just like in most other programming languages, the index starts with 0. So, to get the value of location, you should type:

document.write(exampleJsonObj.countries[0].location);

To get all the ‘languagesSpoken’, type:

document.write(exampleJsonObj.countries[0].languagesSpoken);

To get a specific language, you can add the index to the languagesSpoken key:

document.write(exampleJsonObj.countries[0].languagesSpoken[1]);

To print the value of entire object, convert the object into string, using the stringify() method, and then print the string:

let jsonString = JSON.stringify(exampleJsonObj);document.write(jsonString);

In summary

JSON is easy to learn, especially if you are familiar with JavaScript. It’s easier to parse JSON as opposed to XML. JSON supports flexible schema. Most of the modern programming languages—like Java, Python, and Ruby—extensively support JSON. JSON is compatible with all the major browsers. It’s an ideal data exchange format for web applications that use JavaScript as front-end and MongoDB as their data platform, as MongoDB stores data in BSON, which offers a few more benefits on top of JSON.

What Is JSON? | A Beginner’s Guide | MongoDB (2024)

FAQs

What Is JSON? | A Beginner’s Guide | MongoDB? ›

JSON is a text-based, light-weight data interchange format used to transmit data from server to client in web applications. It's a convenient method of data transport because of its similarity to JavaScript objects. A simple example of JSON is: { “countryName” : “Mexico”, “countryCode” : “+52” }

What is JSON for beginners? ›

JavaScript Object Notation (JSON) is a standard text-based format for representing structured data based on JavaScript object syntax. It is commonly used for transmitting data in web applications (e.g., sending some data from the server to the client, so it can be displayed on a web page, or vice versa).

What is JSON in layman's terms? ›

JSON is neither a file nor a code. Instead, it's a simple format used to store and transport data. It is a plain-text format, which allows for easy data interchange between different programming languages.

How difficult is JSON? ›

JSON (JavaScript Object Notation) is a text-based format for storing and exchanging data in a way that's both human-readable and machine-parsable. As a result, JSON is relatively easy to learn and to troubleshoot.

What is the JSON programming language used for? ›

JSON (JavaScript Object Notation) is commonly used for data storage and transfer. JSON is a popular choice for applications that benefit from a simple and easy-to-use data format. XML (Extensible Markup Language) is a general-purpose markup language similar to JSON that allows for more complex data structures.

How long does it take to learn JSON? ›

Take a JSON Course

Some last just 11 minutes, while some take four weeks to complete. Students can also research coding bootcamps and degrees in fields such as computer science if they want to learn more about JSON.

Why is JSON so popular? ›

JSON is fully language-independent, enlisting the support of other modern programming languages like PHP, Python, or Java. To put it simply, it's a human-readable information exchange format that is generally applied to transfer data between a server and a web solution (a server-client interaction).

What does the JSON format look like? ›

A JSON object contains zero, one, or more key-value pairs, also called properties. The object is surrounded by curly braces {} . Every key-value pair is separated by a comma. The order of the key-value pair is irrelevant.

What is the main function of JSON? ›

Why Use JSON? The JSON format is syntactically similar to the code for creating JavaScript objects. Because of this, a JavaScript program can easily convert JSON data into JavaScript objects. Since the format is text only, JSON data can easily be sent between computers, and used by any programming language.

Why is JSON better than XML? ›

JSON has smaller file sizes and faster data transmission. XML tag structure is more complex to write and read and results in bulky files. JSON is safer than XML.

Is JSON becoming obsolete? ›

JSON is undeniably here to stay. With JavaScript as the most widely used development platform today, JSON has become the most prominent data interchange format.

Should I learn JSON or Python? ›

Python supports a wider range of data types such as lists, tuples, sets, and more, whereas JSON has a more limited set of data types, mainly consisting of strings, numbers, booleans, arrays, and objects. Python's broader range of data types allows for more versatile and complex data structures.

Is JSON better than SQL? ›

JSON is popular among developers for its lightweight nature and for transmitting data between systems. Whereas SQL is used for interacting with the relational database, it supports relational operations, CURD operations, and data integrity constraints.

How is JSON pronounced? ›

Naming and pronunciation

The 2017 international standard (ECMA-404 and ISO/IEC 21778:2017) specifies that "JSON" is "pronounced /ˈdʒeɪ.sən/, as in 'Jason and The Argonauts'". The first (2013) edition of ECMA-404 did not address the pronunciation.

What are the disadvantages of JSON? ›

Limitations of JSON format

No comments - These two words are enough to make the developer's life difficult. Security - JSON can be dangerous if used with untrusted browsers or services. JSON service returns a JSON response, which the browser uses directly, and if the browser is not secure, it can be hacked.

Where is JSON mostly used? ›

The syntax is straightforward to learn, lightweight, and compatible with human and machine languages. The most common uses of JSON include: It is used in writing JavaScript-based applications that have websites and browser extensions as part of their features.

What is a common use of JSON? ›

A common use of JSON is to read data from a web server, and display the data in a web page.

What is the use of JSON simple? ›

simple is a simple Java based toolkit for JSON. You can use JSON. simple to encode or decode JSON data.

How does a JSON format look? ›

A JSON object contains zero, one, or more key-value pairs, also called properties. The object is surrounded by curly braces {} . Every key-value pair is separated by a comma. The order of the key-value pair is irrelevant.

What is difference between REST API and JSON? ›

REST does not have a standardized caching mechanism and leaves it up to developers to implement caching strategies. Sorting and Filtering: JSON API provides standardized parameters for sorting and filtering resources, allowing clients to specify sorting criteria and filter conditions directly in the request URL.

Top Articles
Latest Posts
Article information

Author: Rob Wisoky

Last Updated:

Views: 5864

Rating: 4.8 / 5 (68 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Rob Wisoky

Birthday: 1994-09-30

Address: 5789 Michel Vista, West Domenic, OR 80464-9452

Phone: +97313824072371

Job: Education Orchestrator

Hobby: Lockpicking, Crocheting, Baton twirling, Video gaming, Jogging, Whittling, Model building

Introduction: My name is Rob Wisoky, I am a smiling, helpful, encouraging, zealous, energetic, faithful, fantastic person who loves writing and wants to share my knowledge and understanding with you.