Working with Javascript Object Notation (JSON)

977 VIEWS

The JavaScript Object Notation (JSON) has swiftly gained popularity and has become an important part of a developer’s tool kit. JSON is a lightweight format for transporting data. It aids in the transfer of data structures to formats that are readable by other languages and technologies. It provides developers with retrievable human-readable data storage. JSON may be used in combination with other languages such as Java, JavaScript, Perl, Python, and many others. When you are working with JSON, it is very easy to understand, requires less code and gives better web service results.

Let’s Go Back in Time, Shall We?

The co-founders of State Software agreed to create a system that used standard browser capabilities and provided an abstraction layer for Web developers. This helped web developers to create Web applications with a persistent duplex connection to a Web server. Hence, keeping two Hypertext Transfer Protocol (HTTP) connections open and recycling them before the standard browser time-outs .

 In 2002, the JSON.org website was launched. Yahoo! began selling JSON-based Web services in December 2005. Ecma International released the first edition of their JSON standard, ECMA-404, in October 2013. Request for Comments (RFC)7159, which superseded RFC 4627 and RFC 7158, became the major reference for JSON’s Internet uses in 2014. Crockford was the individual who actually first defined and popularized the JSON format. (See source for more info).

JSON Syntax

The JSON format which is a syntactic subset of the Javascript language is comparable to the Javascript object in that it contains a key-value pair. A comma separates one “key-value” pair from another, while a colon separates the key from the value, all placed in between curly braces. In JSON, the keys are written in double quotes as strings while values must be one of the following data types: boolean, strings, number, array, null or even an object.

The following is a basic JSON syntax format:

{"name":"Torriana"}

Here is another example of JSON’s structure:

{
    "firstName": "Smith",
    "lastName": "Steve",
    "age": 45,
    "children": [],
    "spouse": null,
    "address": {
        "street": "89 mendely",
        "city": "Portharcourt city",
        "state": "Rivers state",
    },
    "phoneNumbers": [
        {
            "type": "mobile",
            "number": "+234 8125 513 799"
        }
    ]
}

JSON Parse

When we are working with JSON, we are using it to exchange data from a web server. The data we receive from a web server is always a string, so it has to be parsed to become a JavaScript object.

JSON parsing is the process of transforming a JSON object in text format to a Javascript object that we can utilize within a program. The conventional way to perform this in Javascript is to use the method JSON.parse(). This method makes the data accessible.

Here is an example:

'{"name":"Larry", "age":39, "city":"New York"}'
 
 
 
const obj = JSON.parse('{"name":"Larry", "age":39, "city":"New York"}');

Prior to processing, we were unable to access the received data, but with parsing, we turned the data into a Javascript object, making the data accessible.

JSON Stringify

The JSON.stringify() method converts a JavaScript object or value to a JSON string. Strings can be used to send data from a client to a server by storing or passing data in a lightweight manner. Value, replacer, and space are the three parameters that json.stringify method accepts.

The value is usually what we transform into a string. The replacer is an option, and the value of its parameter might be a changing function or an array that we can use as a filter for stringify. The space parameter is also optional. The space in the final string generated by the JSON.stringify() function is controlled by this argument.

Here is an example:

const obj = {name: "Peter", age: 23, city: "New York"};
 
//convert into json
const myJSON = JSON.stringify(obj);

Conclusion

JSON has increased in popularity because it makes data transport so simple. It’s also light and simple to read. In fact, JSON’s language is so simple that it’s unlikely to change! As a result, if you haven’t tried working with JSON already, you should consider doing so today.


Faith Njah is a front-end developer, with experience in building dynamic websites using web technologies like HTML, CSS, JavaScript, and A-frame VR. She is also passionate about learning.


Discussion

Click on a tab to select how you'd like to leave your comment

Leave a Comment

Your email address will not be published.

Menu
Skip to toolbar