JSON
--
JSON is a data format used for easy storage, transmission, and retrieval of data. It stands for JavaScript Object Notation and is closely associated with JavaScript, although it can be used with different programming languages as well. It doesn’t require knowledge of JavaScript and is independent of any specific programming language. JSON is used to facilitate data interchange between humans and computers. It has gained worldwide popularity and is widely used in APIs and Configurations.
JSON SYNTAX
JSON objects are defined within curly braces ({}). Each object consists of key-value pairs. Keys are defined as text within double quotes (“”) and must be unique. Values are located to the right of the key and can take different data types. JSON values can include:
Text (string): Text data enclosed in double quotes (“”).
Number: Natural numbers, decimal numbers, or numbers in scientific notation.
Boolean: True or false.
Array: A list of values separated by commas and enclosed in square brackets [].
Object: A data structure consisting of key-value pairs enclosed in curly braces {}.
Null: Undefined or empty value.
EXAMPLE OF JSON CODE
{
"name": "John",
"surname": "Doe",
"age": 30,
"active": true,
"hobbies": ["yüzme", "müzik dinleme", "kitap okuma"],
"adress": {
"city": "İstanbul",
"district": "Kadıköy"
},
"phones": [
{
"type": "ev",
"number": "555-1234"
},
{
"type": "iş",
"number": "555-5678"
}
]
}
In this example, there are basic user information such as name, surname, and age, as well as more complex data structures like hobbies, address, and phone numbers.
ALTERNATIVES TO JSON
There are other commonly used data formats besides JSON, such as XML (eXtensible Markup Language), YAML (YAML Ain’t Markup Language), and CSV (Comma-Separated Values). These formats are widely used for data representation and storage, offering different features and use cases compared to JSON.
COMPARISON BETWEEN JSON AND XML
JSON and XML use different syntaxes to represent data. JSON is a JavaScript-based data format and is represented using key-value pairs, while XML represents data using tags and opening-closing tags. JSON is generally preferred in web services and modern applications because it is lighter and has less syntactic complexity.
JSON is a natively supported data format in the Swift language. With the Codable protocol, we can easily convert JSON data to Swift objects or Swift objects to JSON data. This makes it easier to process and use JSON data.
On the other hand, we may need to use an additional library in the Swift language to process XML data. Third-party libraries like SWXMLHash provide an easy way to convert XML data to Swift objects. This enables us to process and use XML data, but it may require a bit more effort.
JSON can have smaller sizes and can be processed faster. XML, on the other hand, can have a more readable structure and can provide better structural data validation in some cases.
In conclusion, JSON is generally the more preferred data format, especially in modern applications and web services. JSON processing is more natural and straightforward in the Swift language. XML may be required in certain scenarios and can be processed using additional libraries in the Swift language. Both data formats offer different advantages in specific scenarios.
Now, let’s compare them using code
SIMPLE JSON EXAMPLE
{
"employee": {
"name": "John Doe",
"age": 30,
"department": "Engineering",
"skills": ["Swift", "JavaScript", "Python"]
}
}
SIMPLE XML EXAMPLE
<employee>
<name>John Doe</name>
<age>30</age>
<department>Engineering</department>
<skills>
<skill>Swift</skill>
<skill>JavaScript</skill>
<skill>Python</skill>
</skills>
</employee>
MALFORMED JSON
The last thing I want to mention is malformed, which is one of the common errors that you may encounter with JSON.
Malformed JSON refers to an invalid or non-compliant JSON data expression that does not constitute a valid JSON document.
Malformed JSON can include the following situations:
Incorrect syntax: A JSON document should begin and end with curly braces `{}`, and key-value pairs should be in the format of `”key”: value`, enclosed in quotation marks. If the syntax does not follow these rules, the JSON is considered invalid.
{ name: "John Doe", age: 30 }
In the above example, since the keys are not enclosed in quotation marks, the JSON is invalid.
Missing or extra delimiters: In a JSON document, key-value pairs are separated by commas. However, missing or extra commas can disrupt the structure of a valid JSON document.
{ "name": "John Doe", "age": 30, }
In the above example, the presence of an extra comma after the last key-value pair makes the JSON invalid.
Type incompatibility: In JSON, keys and values should represent specific data types. For instance, a key should be a string, while a value should not be a number or a boolean.
{ "name": "John Doe", "age": "30" }
In the above example, the value for “age” is a string instead of a number, making the JSON invalid.
and we come to the end of the article…
If you want to see a real JSON code used in Swift, I made a small application by referring to Atıl Samancıoğlu’s course at BTK Academy as a reference. You can access the code by clicking the GitHub link.
https://github.com/muratyildirimm/NewsAppWithJSON/tree/main
I hope the article was informative. See you in the next article :)