Free online JSON formatter tool with advanced configurations

Mirror, validate, compare, sort, format, minify, unminify, transform, beautify, search & more.

+
-

JavaScript Object Notation (JSON)

Today we are going to take a look at something new that has arrived in the programming world, JavaScript object notation or JSON for short. Many consider this the future of web development because it will eliminate the need to use HTML tags to format your documents and data sets.

The way it works is pretty simple – you just add square brackets after each property (variable name) and then a comma and the value of that property.

For example, say you wanted to create an object with two properties called color and shape. You could write it like this:

{[color], [shape]}

That would be equivalent to saying “an object containing a single variable named color and a single variable named shape”.

Now, let’s apply that to some practical examples. Say you want to make a box, and inside of that box you want to put another box. The inner box should have four sides and one line segment as its top face.

We can make our outer box into an object by adding [] before the word color and putting a, in between the [[]] and the next item. We do the same thing for the bottom face but instead of using 4 lines we use 1 so it becomes a circle.

So our full, well-formed object looks like this:

{[[color]], [[shape]]}

This creates a box with a color of red and a shape of a cube.

Examples of JSON

In our last article, we took a look at why you should use Javascript’s native object literal notation (or “dot notation”) when creating objects.

Now it is time to take a closer look at how you would create some basic objects using this method.

We will be taking a simple example of an employee structure with two properties – first name and last name. We will also add a few methods such as getFullName(), which returns the full name of the employee, and sayHello() which simply prints out the employee’s first name followed by their last name.

Creating the Employee Objects

To begin defining our employees, let's open up your HTML editor and start editing. First, we will insert a new element named empList, set its value to "EMP LIST", and then for the class attribute we will type `employee'. This sets up the list style to be like a normal unordered list.

Next, we will define our first item in the list by inserting another new tag and giving it a nested key/value pair called

  • with id="1" and content=". John Doe - Full Name.".

    This says that the List Item has an ID of 1, and the content inside is ". John Doe - Full Name.". Note that there are no quotes around the value; these have been inserted for us.

    What is JavaScript Object Notation?

    So what is JSON, you may ask? Well, it’s just an easy way to encode data in a web-based language. Technically speaking, it’s called JavaScript object notation (JSON). It was first described by Doug McIllhenny in 2005 as a more human friendly approach to encoding information.

    By defining variables with dashes instead of brackets, it becomes much easier to read which makes it much better than using square braces or parentheses. For example, say you wanted to define two attributes named ‘first name’ and ‘last name’ that were both set to equal one value – John. You could do this very easily using curly brackets, but it would be a lot harder to understand!

    So how does JSON compare to other ways of defining objects in your software? Well, aside from being much cleaner, it is also slightly faster to process.

    Examples of JSON

    Today, we are going to take a look at some examples of what people refer to as JSON – JavaScript object notation. Technically speaking, this is not quite right. What most people mean when they say “JSON” is something closer to Javascript-style objects with nested key/value pairs.

    That being said, it is fine to use the term “JSON” for these concepts! The reason why it is important to define the term properly before using it is because there is already an established standard for what JSON means.

    There are several different standards for what JSON should be. Some include:

    The main difference between these definitions is how defining keys and values work. In general, simpler is better in terms of usability, but more concisely defined formats are slightly more powerful. It depends on your needs which one you want to stick with.

    In this article, we will focus only on very basic ideas of JSON that anyone can understand. We will talk about things like creating new objects, changing existing properties, and reading content from other files or classes.

    To make sure everything goes smoothly, let’s start by taking a quick review of some common terminology first.

    How do I use JSON?

    A quick reminder: JSON is just a way to encode data in a human-readable format or decode it back into a usable form.

    So, how do you use JSON? That question has been answered before! Check out our article here where we gave some examples using JSON with numbers, strings, arrays, objects, and files.

    Now that you know what each part of the JSON looks like, creating your own can be tricky. Luckily, there are software packages that make this process easy for novices and experts alike!

    There are many free and paid apps available that create these basic components for you. Some even have powerful features such as automatic formatting, validation, and conversion from/to other formats.

    What are the limitations of JSON?

    A more advanced feature of JSON is called object notation, or defining your own set of keys to represent different properties in an object.

    This allows you to have much longer keys than the typical number that limit how long a key can be. For example, let’s say we want to create a product object with two attributes: name and price. We could simply use the strings “name” and “price” as our keys for this object. However, what if these objects get larger and need additional attributes to describe them?

    We could run into problems later when trying to access those extra attributes because there would be no way to know which part of the string represents which attribute!

    Object notation solves this problem by allowing us to define our own sets of characters to use as keys instead of just using alphabetic characters.

    For example, what if we wanted to make an object that represented a person? We could define a key named `firstName` that was limited to only containing lowercase letters. Then, we could define a second key named `lastInitial` that was also limited to only containing lowercase letters but needs to be followed by an uppercase letter.

    What are some good use cases for JSON?

    Another common way to use JSON in web applications is as an object notation, or key-value pairs. Rather than using strings to represent keys and values, you can instead make it work like Python or Java, with objects!

    This is how it works:

    You have a variable that contains your JSON data. The variable could be called json, oject, or anything else you’d like to call it. Then you take this variable and insert it into another piece of software, which uses the contents of this new variable to do something.

    Software that uses JSON as an object notation usually has users create objects within their program by doing something similar to what we did before. They type in the property name (key) then press enter or choose an already created item (value). When they hit save, the software converts these properties into a valid JSON string, which is then inserted into the original variable.

    What are some bad use cases for JSON?

    Another common use case for JSON is data in databases or other formats that have to be pasted into a browser, web page, app, etc. While this can work okay if you are careful about it, there are better ways to do it.

    Data in these formats usually come with a toolkit of functions to access and process the information. If you need to add more functionality later, you will probably have to re-save the formatted data as something new (such as using SQLite instead of MySQL).

    This happens a lot more than most people realize! It’s very possible to find yourself running into this problem.

    If you ever run into this issue, try looking up how to convert your database/format file into plain text first.

    Can I use JSON with my server-side language?

    Yes, you can! In fact, it’s very common to do so now. Many people begin using JSON as their data format before moving onto more advanced formats like XML or YAML.

    By adding a simple encoding/decoding process in your web browser (or other compatible software such as Google Chrome), you are already well on your way to mastering JSON.

    A lot of languages have built-in support for JSON, making this conversion pretty easy. Most popular programming languages come with some kind of native object that make working with JSON a breeze.

    For example, PHP has an array called json_encode() which turns any type into an easily manipulable JSON string.

  • More free tools from Malgo