I have JSON response as given below:
{
"status": true,
"data": [
{
"id": 2,
"name": "demo",
"last_name": "test"
},
{
"id": 6,
"name": "test",
"last_name": null
},
{
"id": 15,
"name": "test",
"last_name": null
}
],
"message": "List fetched successfully"
}
From the above response, I would like to create an array of strings which is a combination of values of the keys
name
and
last_name
from the above-given response. How can I achieve array of strings as given below:
["<name last_name>","<name last_name>","<name last_name>"]
Your help will be appreciated.
First you need to create your structs to decode the JSON (note that I'm only appending last_name if it's not nil):
Once you decode your JSON, you can obtain the array you want by using the map function: