Parsing a JSON response
Now that we have successfully retrieved a JSON response from an API, it is time to learn how to use the data we have obtained. To do so, we need to parse the JSON payload. This is because the payload is a plain string representing the data object, and we are interested in the specific properties of that object. If you look closely at Figure 5.2, you may notice that the JSON contains a unique identifier for each result, an image URL, and image dimensions. For our code to use this information, we must extract it.
As mentioned in the introduction, multiple libraries exist that will parse a JSON payload for us. Some of the most popular ones are Google’s Gson (https://packt.link/uFRVJ) and Square’s Moshi (https://packt.link/LpZ9N). Recently, Kotlin Serialization (https://packt.link/kbYmZ) has been gaining traction. The main reasons for this trend are its support of Kotlin Multiplatform and its lightweight nature. Due to this, we will use Kotlin...