Difference between revisions of "W2271 JSON FILES"

From Coder Merlin
Line 13: Line 13:
==What are the benefits and shortcomings of JSON?==
==What are the benefits and shortcomings of JSON?==


JSON is almost a subset of the object literal notation of JavaScript. Because it can be used to represent JavaScript object literals, JSON commonly serves as a data-interchange language. In this it has much in common with XML. Because it is JavaScript notation, JSON can often be used in JavaScript programs without any need for parsing or serializing. It is a text-based way of representing JavaScript object literals, arrays, and scalar data.
Due to how frequently JSON is used with coding languages such as Swift, it's almost becoming a subset to these primary coding languages. Since JSON commonly serves as a data-interchange language and is used in high frequency with Swift applications that leverage API-based webservices, let's explore some of the major benefits for knowing JSON as you develop your coding career, and some disadvantages as well.


===Advantage of JSON===
===Advantage of JSON===
'''1. JSON is Faster''' - JSON syntax is very easy to use. We have to use only as a syntax which provide us a easy parsing of the data and faster execution of the data. Since its syntax is very small and light weighted that's the reason that it executes the response in faster way.
'''2. Server Parsing''' - On the server side parsing is the important part that developers want if the parsing will be fast on the server side then only user can get the fast response of their response so in this case JSON server side parsing is strong point that indicates us to use the JSON on server side.
'''3. Schema Support''' - It has wide range of supported browser compatibility with the operating systems so the applications made with the coding of JSON doesn't require much effort to make it all browser compatible. During development developer thinks for the different different browsers but JSON provides that functionality.
'''4. Tool for sharing data''' - JSON is the best tool for the sharing data of any size even audio, video etc. This is because JSON stores the data in the arrays so data transfer makes easier. For this reason, JSON is a superior file format for web APIs and for web development.
===Disadvantage of JSON===
'''1. JSON is Faster''' - JSON syntax is very easy to use. We have to use only as a syntax which provide us a easy parsing of the data and faster execution of the data. Since its syntax is very small and light weighted that's the reason that it executes the response in faster way.
'''1. JSON is Faster''' - JSON syntax is very easy to use. We have to use only as a syntax which provide us a easy parsing of the data and faster execution of the data. Since its syntax is very small and light weighted that's the reason that it executes the response in faster way.



Revision as of 09:34, 20 November 2021

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder

What is the motivation for learning JSON?[edit]

 This article can be improved by:  Beginning the article with the motivation for learning the topic.

There are several reasons learning JSON can be beneficial for developers, especially those early in their coding career. First off, learning JSON gives developers an easy path to working with a multitude of web-based applications and services that are written in Javascript. Since JSON is a superset of JavaScript, anything you write in JSON can be interpreted by JaveScript compliers, lowering the barrier for entry to working with Javascript-based applications and services. Furthermore, since JSON is a text-only data-interchange format, it is intuitive and the intention of a snippet of JSON can be interpreted easily. Further, nearly every major coding language today has a JSON library allowing JSON to be injected and interpreted by compliers independent of the coding language you wish to write in.

What is JSON?[edit]

 This article can be improved by:  Emphasizing in italics important concepts, e.g. open standard method of storing, sending, and retrieving data

JSON or Javascript Object Notation is a text-based language-independent data-interchange format very similar to XML or YAML. Although JSON can be used with almost every major coding language, JSON wouldn’t be correctly classified as a “programming language” per se, rather it’s a text-based open standard method of storing, sending, and retrieving data between a client and server.

JSON is a very important data representation format, as it is widely used to facilitate the majority of web-based API calls and is very common in facilitating communication between servers and clients in a wide array of applications.

When using JSON in Swift, you can think of JSON as a dictionary that maps keys to unique values. These values can be strings, numbers, bools, or null (nothing). And for that iOS developer early in their career, it's very important to consider learning JSON as you'll come across it quite often when working building applications that interact with webservices. For instance, major web platforms such as Facebook to Twitter, use JSON to transmit data to and from your application.

What are the benefits and shortcomings of JSON?[edit]

Due to how frequently JSON is used with coding languages such as Swift, it's almost becoming a subset to these primary coding languages. Since JSON commonly serves as a data-interchange language and is used in high frequency with Swift applications that leverage API-based webservices, let's explore some of the major benefits for knowing JSON as you develop your coding career, and some disadvantages as well.

Advantage of JSON[edit]

1. JSON is Faster - JSON syntax is very easy to use. We have to use only as a syntax which provide us a easy parsing of the data and faster execution of the data. Since its syntax is very small and light weighted that's the reason that it executes the response in faster way.

2. Server Parsing - On the server side parsing is the important part that developers want if the parsing will be fast on the server side then only user can get the fast response of their response so in this case JSON server side parsing is strong point that indicates us to use the JSON on server side.

3. Schema Support - It has wide range of supported browser compatibility with the operating systems so the applications made with the coding of JSON doesn't require much effort to make it all browser compatible. During development developer thinks for the different different browsers but JSON provides that functionality.

4. Tool for sharing data - JSON is the best tool for the sharing data of any size even audio, video etc. This is because JSON stores the data in the arrays so data transfer makes easier. For this reason, JSON is a superior file format for web APIs and for web development.

Disadvantage of JSON[edit]

1. JSON is Faster - JSON syntax is very easy to use. We have to use only as a syntax which provide us a easy parsing of the data and faster execution of the data. Since its syntax is very small and light weighted that's the reason that it executes the response in faster way.

2. Server Parsing - On the server side parsing is the important part that developers want if the parsing will be fast on the server side then only user can get the fast response of their response so in this case JSON server side parsing is strong point that indicates us to use the JSON on server side.

3. Schema Support - It has wide range of supported browser compatibility with the operating systems so the applications made with the coding of JSON doesn't require much effort to make it all browser compatible. During development developer thinks for the different different browsers but JSON provides that functionality.

4. Tool for sharing data - JSON is the best tool for the sharing data of any size even audio, video etc. This is because JSON stores the data in the arrays so data transfer makes easier. For this reason, JSON is a superior file format for web APIs and for web development.

JSON vs. XML[edit]

JSON in Swift[edit]

There's a very strong use case for considering JSON when working in Swift. Take for example, if you are working on an app that is required to transmit data to and from a webservice via API. Every time you wish to send or receive data to and from this webservice, your Swift code must be encoded to and from JSON to facilitate this communication. This is one of JSON's most profound capabilities. With JSON developers are able to encode any data format in JSON, and decode JSON back to any data format, simply by using some predefined protocols.

This built-in support for translating code to and from JSON and Swift is known as codable. When a piece of JSON code is considered codable, developers can freely convert data into usable swift code in a very simple and straightforward manner. Importantly, these protocols come in very handy for sending and storing data with JSON.

And once a Swift object is defined as "Codable" it can then be easily encoded and decoded to facilitate API-based transmission to webservices.

  • Encodable - When a piece of JSON code is Encodable, you're signifying that this data can be converted to a JSON object. Most commonly, this is used to send data to the server.
  • Decodable - By marking your model type as Decodable, you're signifying that this JSON object can be converted to a Swift value. Most commonly this is used when receiving data from the server.

Before digging into some of the Swift protocols, let's start with a simple example of a JSON object with five keys. And for means of example, let's assume you're working on an app that uses professional athletes' stats in some facet.

You may have a Swift object that follows the given structure:

struct Player Codable {
    var name: String
    var position: String
    var url: URL
    var number: Int
    var bio: String
}

And to show how to translate data between Swift and JSON, let's write some accompanying JSON.

{
    "name":     "LeBron James",
    "position":   "Shooting Guard",
    "url":      "https://www.nba.com/player/2544/lebron-james",
    "number":    6,
    "bio": "Four-time NBA Champion (2012, 2013, 2016, 2020) … Four-time NBA Most Valuable Player (2009, 2010, 2012, 2013) … Four-time NBA Finals MVP 
     (2012, 2013, 2016, 2020)",
}

Since JSON data is typically received and sent by webservice APIs as a string, let's to one step further to translate this JSON text into a string.

let jsonString = """
{
    "name":     "LeBron James",
    "position":   "Shooting Guard",
    "url":      "https://www.nba.com/player/2544/lebron-james",
    "number":    6,
    "bio": "Four-time NBA Champion (2012, 2013, 2016, 2020)  Four-time NBA Most Valuable Player (2009, 2010, 2012, 2013)  Four-time NBA Finals MVP 
     (2012, 2013, 2016, 2020)",
}
"""

Once a JSON string is created, developers can start by converting this JSON string to a user object, a critical step to begin using the decoder and encoder protocols.

let jsonObject = jsonString.data(using: .utf8)!

Now, by using JSONDecoder, we can create a new object as such:

let jsondecoder = JSONDecoder()

Now that the decoder object is created, it's now possible to decode the JSON data as follows:

let user = try! decoder.decode(User.self, from: jsonData)


JSON Encoding[edit]

CoderMerlin™ Code Explorer: W0000 (1) 🟢


JSON Decoding[edit]

CoderMerlin™ Code Explorer: W0000 (2) 🟢


Using JSON with JavaScript[edit]

 This article can be improved by:  Providing initial examples in Swift using the CodeExplorer. While JSON is most certainly used with JavaScript, because this site is primarily focused on Swift, begin the examples there.

Converting JSON to Javascript is quite easy. In fact, there are a series of Javascript functions that can be called to parse JSON into a Javascript object.

With JSON.parse(), for instance, developers can input a JSON string and parse the text into a Javascript object.

'{"name":"Bob", "age":20, "city":"Phoenix"}'

Using the JSON.parse() function, one can simply pass the JSON to the function as follows:

const obj = JSON.parse('{"name":"Bob", "age":20, "city":"Phoenix"}');

By doing so, JSON.parse() will create a Javascript object, in this case, a const type object called obj to how the JSON values in a Javascript JSON format.

Next, using the JSON.stringify() function, the object created via the JSON text using JSON.parse() can be converted to a string.

const obj = {name: "Bob", age: 20, city: "Phoenix"};

Again, results in a const type object with the given values. And now, that object can be converted to a string.

const myJSON = JSON.stringify(obj);