Difference between revisions of "W2271 JSON FILES"

From Coder Merlin
Line 6: Line 6:


==JSON in Swift==
==JSON in Swift==
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 into JSON to meet the
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.


developers have access to protocols that allow Swift objects such as a '''String''' , '''Int''' , '''Double''' , '''URL''' , '''Date''' , '''Array'''  and '''Dictionary''' to be easily encoded into JSON for sending and receive data from a webservice.  
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.


* '''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.


Swift offers built-in support for working with JSON through a protocol called 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.
* '''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.  


* '''Encodable''' - By marking your model type as Encodable, you tell Swift that you want to use this type to convert instances of Person to a JSON object. Most commonly this is used to send data to the server.
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:


* '''Decodable''' - By marking your model type as Decodable, you tell Swift that you want to use this type to convert JSON to Person instances. Most commonly this is used when receiving data from the server.
<syntaxhighlight lang="swift">
struct Player {
    var name: String
    var position: String
    var url: URL
    var number: Int
    var bio: String
}
</syntaxhighlight>


*'''Codable''' - As mentioned earlier in this article, Codable is a type alias for both Encodable and Decodable which allows your type to be used to encode and decode JSON. Codable was introduced in Swift 4. Whether you mark your type as Encodable, Decode, or Codable, is up to you. Some may prefer to be explicit with their naming and stick with the Encodable and Decodable, but just mark as Codable unless told otherwise.
In Swift, if we want to convert this object to JSON we would use the encoding protocol which will allow developers to encode the Swift object to JSON making it seamless for JSON-based APIs to use this data for communication with web services. Following the use of encode, we will get the following JSON data.
 
<syntaxhighlight lang="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)",
}
</syntaxhighlight>


==What is the motivation for learning JSON?==
==What is the motivation for learning JSON?==

Revision as of 07:04, 20 November 2021

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

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.

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.

  • 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.

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 {
    var name: String
    var position: String
    var url: URL
    var number: Int
    var bio: String
}

In Swift, if we want to convert this object to JSON we would use the encoding protocol which will allow developers to encode the Swift object to JSON making it seamless for JSON-based APIs to use this data for communication with web services. Following the use of encode, we will get the following JSON data.

{
    "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)",
}

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.

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);