Some rules for writing JSON Data

In this tutorial, we are going to understand what are the rules for writing JSON data. As you might have heard, JSON or JavaScript Object Notation is a widely used file format for data storage and interchange. There are many reasons why JSON is a widely used file format.

Some rules for writing JSON Data

When you need to write some data in a JSON file, there are some rules that you need to follow. So, here are the rules –

  • The data in JSON is stored as name:value pair.
  • The keys must be enclosed in double quotes.
  • The key and value should be separated by colon (:)
  • Unlike some programming languages, no comments are allowed in JSON data.
  • We can have multiple key:value pairs, just we need to separate them with commas.
  • Objects in JSON are surrounded by curly braces. ({})
  • The JSON array is surrounded by square brackets. ([])
  • The value can be some valid data.

So, these are some of the rules that we need to follow, while we are working with JSON data. Now, let’s move on to some examples, where we are going to practically use these rules, to write the JSON data. While we would use these rules in action, you would understand them, and it would be easier for you to implement them later.

Example 1 –

In this example, let’s say that we have some data of a user from a movie streaming platform, like the name of the customer, age, whether or not he has subscribed, and the movie categories that the user has selected, or likes to watch. Here is an example.

{
    “name” : “GyaniPandit”,
    “age” : 25,
    “subscribed” : true,
    “movie_categories”: [“Drama”, “Horror”, “Fiction”, “Biography”]
}

As you can see in the above example, we have some simple JSON data. Many of the rules that we have read above are applied here in the example. The data here is in the form of key:value pairs. There are multiple key:value pairs, so we have separated them with commas. The key is enclosed in the double quotes. The value is some kind of valid data. Also, we have the object surrounded by curly braces, and the array is surrounded by square brackets.

So, from this example, we can understand multiple rules being used. The above example is very simple, so you can also consider it as a basic example, to understand how can we write JSON data. Here, “name” is the key, and the corresponding value is “GyaniPandit”. So, we can say that there is a key, and we have a value associated with that key.

If you are familiar with some other programming languages, like Python, you might be knowing about the dictionary, which also has key:value pairs, or JavaScript, in which we have objects, which we have properties.

The thing is that JSON text format is completely language-independent, but it uses conventions from many programming languages from the C family, like C, C++, Java, JavaScript, Python, Perl, and many others.

Example 2 –

Let’s have a look at another example, through which, we can have a more clear idea about the rules for writing the JSON data. In the below example, we again have the customer data, but this time, you would be seeing quite more data. But we would break it down in simple terms, to understand the data.

{
“name” : “GyaniPandit”,
“age” : 25,
“email”: “[email protected]”,
“isPremium”:true,
“orders”:[
{
“product_id”:123,
“product_name” : “Wireless Earphones”,
“product_price” : 999,
“product_quantity”: 1,
“isGift”: false,
“payment_status” : “prepaid”
},
{
“product_id”:133,
“product_name” : “School bag”,
“product_price” : 899,
“product_quantity”: 1,
“isGift”: true,
“payment_status” : “prepaid”
}
]
}

In the above example, there are some usual things that we are already familiar with, like multiple key:value pairs being separated by commas, keys being enclosed in double quotes, or objects being surrounded by curly braces. But here, you can see that for the “orders” key, the value is an array, in which we have objects. To simplify this, the objects in the array can be considered as just individual order details, like the product name, price, and some other details.

So, at times, when we are required to work with some JSON data, we should follow the above-stated rules. From the above examples, we hope that you have understood the rules, and as you would work with more JSON data, you would become more familiar with the rules, and the syntax as well.

If you wish to know more about JSON or learn more about JSON, you can try our course on JSON, or JavaScript Object Notation, where you would learn and understand many concepts related to JSON, like how to write JSON data, work with different types of data in JSON file, different examples, and much more.

Along with that, we have a lot of other courses related to different programming languages and many other tools, and we continuously keep on adding new courses and videos on our different platforms. So, you can explore our courses, and upskill yourself with us.

FAQ About rules for writing JSON Data

Q: What is the full form of JSON?

Ans: The full form of JSON is JavaScript Object Notation.

Q: Who created JSON?

Ans: JSON was created by Douglas Crockford.

Q: What is JSON?

Ans: JSON, or JavaScript Object Notation, is a lightweight data-interchange format, which is easy for humans to read and write. It is completely language-independent, but uses many conventions from the C language family, like C, C++, Python, Java, etc.

Q: What are some rules for writing JSON data?

Ans: When we are working with JSON data, we need to follow some rules, like –

1. data is stored as name:value pair.
3. 2. The keys must be enclosed in double quotes.
4. The key and corresponding value are separated by a colon.
5. There are multiple key:value pairs, separated by commas.
6. No comments are allowed in JSON files.
7. The value can be some valid data.