March 28, 2024

Stock Bitcoin

Bitcoin News and something more!

Introduction to JSON – What is a JSON file?

what is a json file

A JSON file is known as the data exchange standard, which implies that it can be used as a data format wherever data exchange occurs.

An exchange of data can occur between the browser and the server and even from server to server, where appropriate. Of course, these are not the only possible means to exchange JSON, and leaving it in those two would be quite limiting.

In summary, JSON is a textual representation defined by a small set of governance rules on which the data is structured. The JSON specification states that the data can be structured as:

1. A collection of tag and value pairs
2. An ordered list of values

Composite structures

Since the JSON origins come from the ECMA Script standardization, the implementations of the two structures are represented in the object and matrix forms. Crockford describes the two structural representations of the object like a series of syntax diagrams.

As described in the diagram, a collection begins with the use of the opening key ({) and ends with the use of the closing key (}). The content of the collection may be composed of any of the following three possible designated routes:

  • The top path illustrates that the collection can remain devoid of any string / value pair.
  • The intermediate route illustrates that our collection can be that of a single chain / value pair.
  • The bottom path illustrates that after a single string / value pair is provided, the collection does not need to end, but allows any number of string / value pairs before reaching the end. Each string / value pair in the collection must be delimited or separated from each other by a comma (,).

For example we can declare JSON in the form of pairs or label / value in this way:

// Empty collection
{}
// One pair {"greeting": "hello world"}

// JSON with multiple tag-value pairs
{"Microsoft": "Bill Gates", "apple": "Tim Cook"}

Or we can also create ordered lists

// Empty sorted list
[]
// Ordered list of a value ["computer"]
// List ordered with several values ​​[0, ”1 ″, 2,3,4,5]

We can also add nested components, such as the examples below:


// With array
[
    {"greeting": "hello"},
    [0, "1", 2, 3, 4, 5]
]
// With object
 {
     "object": {
         "array": [true]
     }
 }
// With array
[
    {"greeting": "hello"},
    [0, "1", 2, 3, 4, 5]
]

// With object
 {
     "object": {
         "array": [true]
     }
 }

How to use a JSON?

Text strings in JSON format help to pass parameters using a single text string, for example to pass all parameters to a variable or attribute to send them through a GET request. For this it is necessary to escape the quotes ” using backslash like this: \”

For example a String whose internal content is a JSON (In the example we use javascript, but the format is also used for other languages)

var stringJson = "{\"greeting\":\"hello world\", \"bye\":\"see you later\"}";
var stringJson = "{\"greeting\":\"hello world\", \"farewell\":\"see you later\"}";

In this way we have only one String, with a Json inside. In Javascript we can save the process of escaping quotes using Stringify and declare the object directly using JSON.stringify:

var person = {name: "Peter", age: 23, city: "London"};
var stringJson = JSON.stringify(person);
console.log(stringJson)

Where person is an object of type and stringJson is a string that contains the escaped object.

And the reverse process?

To convert (or parse) a string back to the object format, it is very simple to use JSON.parse:

var stringJson = "{\"name\":\"Peter\", \"age\":\"23\", \"city\": \"London\"}";
var person = JSON.parse(stringJson);
document.getElementById("demo").innerHTML = person.name;

Where we get as a result the person’s name.

These kind of codes are handled through levels, the escaping and parsing process is carried out at each level, as long as at each level we have an associated (as in the case of the nested Jdon). At another time we will explain how to parse a nested Json.