What is JSON and how do you parse JSON in Swift?
Think about how a dictionary is organized alphabetically and each entry has a word followed by a definition. There's a well defined standard which makes it easy to process the info.
The JSON data format is often used in a data feed or in the returned data from an API call.
Because the data is in a standard data format, our app can "parse" out the information from the data feed or API response.
To understand how JSON is organizes data, you need these 3 concepts:
Key-Value pairs: The key is a label and the value is the actual data. Together, a key-value pair represents one piece of information.
JSON Object: This is a collection of key-value pairs together. Often the key-value pairs are related together so that the collection (JSON object) represents a bigger piece of data. For example, the JSON Object could represent a person and the collection of key-value pairs represents the attributes of the person.
Array: An array in JSON represents a list of JSON objects or other values.
For an example of JSON data, check out the video tutorial! In the video, I also show how to parse JSON in Swift using the Codable protocol.
We take the response JSON data from a real API call and then parse it in Swift!
JSON.stringify() and JSON.parse() are useful tools for handling JSON-formatted content