How to Parse This JSON in Swift

How to parse a JSON file in swift?

This answer was last revised for Swift 5.3 and iOS 14.4 SDK.


Given some already obtained JSON data, you can use JSONDecoder to decode it into your Decodable model (or a collection of models).

let data: Data = /* obtain your JSON data */
let model = try JSONDecoder().decode(Model.self, from: data)

Such model must conform to the Decodable protocol and contain correct mapping between properties and JSON dictionary keys. As an example, consider the following JSON array containing search results of cities beginning with "Wa".

[
{
"id": 123,
"city": "Washington",
"region": "D.C.",
"country": "United States"
},
{
"id": 456,
"city": "Warsaw",
"region": "Mazowieckie",
"country": "Poland"
},
...
]

For that, you need to create a model that contains the correct properties of correct types. If you're using a web API, its documentation will be of great help here.

struct SearchResult: Decodable {
let id: Int
let city: String
let region: String
let country: String
}

Then decode the data with JSONDecoder:

let results = try JSONDecoder().decode([SearchResult].self, from: data)

Given a new array of decoded search results, call one of UITableView's functions to reload its data. Note that the decode function can throw an error which you must somehow handle.

To learn more about decoding custom types in Swift and more advanced usage of the Codable APIs, I recommend checking out this documentation article.

How to parse this nested json in Swift 5?

You can use a service like quicktype to create Codable classes. As a starting point you should read more about the Codable Protocol

You can use the following code to parse this JSON:

import Foundation

// MARK: - Root
struct Root: Codable {
let status: Int
let data: [Datum]
let message: String
}

// MARK: - Datum
struct Datum: Codable {
let month: String
let jobs: [Job]
}

// MARK: - Job
struct Job: Codable {
let jobID: Int
let jobTitle, jobDesc, jobDate, jobVenue: String
let jobAccept: String

enum CodingKeys: String, CodingKey {
case jobID = "jobId"
case jobTitle, jobDesc, jobDate, jobVenue, jobAccept
}
}

You can use this code to convert the JSON to an object:

let root= try? JSONDecoder().decode(Root.self, from: jsonData)

How to parse JSON using swift 4

The issue you are facing is because your JSON is returning different data for your Fruits.

For the 1st ID it returns a String called name, but in the 2nd it returns a String called title.

In addition when parsing the JSON the ID appears to be a String and not an Int.

Thus you have two optional values from your data.

As such your Decodable Structure should look something like this:

struct Response: Decodable {
let fruits: [Fruits]

}

struct Fruits: Decodable {
let id: String
let image: String
let name: String?
let title: String?
}

Since your URL doesn't seem to be valid, I created the JSON file in my main bundle and was able to parse it correctly like so:

/// Parses The JSON
func parseJSON(){

if let path = Bundle.main.path(forResource: "fruits", ofType: "json") {

do {
let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe)
let jsonResult = try JSONDecoder().decode(Response.self, from: data)

let fruitsArray = jsonResult.fruits

for fruit in fruitsArray{

print("""
ID = \(fruit.id)
Image = \(fruit.image)
""")

if let validName = fruit.name{
print("Name = \(validName)")
}

if let validTitle = fruit.title{
print("Title = \(validTitle)")
}

}

} catch {
print(error)
}
}
}

Hope it helps...

How can I parse a JSON string in swift?

Here is how you can decode your response:

typealias YourDecodedObject = [ClassObject]

struct ClassObject: Codable {
let welcomeClass: String?
let classToken: String?
let teacherName: String?
let room: String?
let grade: String?

enum CodingKeys: String, CodingKey {
case welcomeClass = "class"
case classToken, teacherName, room, grade
}
}

func decodeResponseFrom(data: Data?) {
if let data = data {
do {
let decodedObject = try JSONDecoder().decode(YourDecodedObject.self, from: data)
} catch {
print("could not decode ❌")
}
}
}

How to parse json in swift (convert json string to string)

You get a json string so you can try

  let jsonstring = "\"asdf\""
let data = jsonstring.data(using: .utf8)

do {
if let str = try JSONSerialization.jsonObject(with: data!, options:.allowFragments) as? String {
print(str)
}

}
catch let caught as NSError
{
}

How to parse nested json? Swift

The JSON is corrupt and you are decoding the wrong root object.

This is the correct JSON being decoded with your structs and conforming to the Swift naming convention

let jsonNestedString  = """
{"meta":{"page":1,"total_pages":4,"per_page":10,"total_records" : 38}, "breweries":[{"id":1234,"name":"saintArnold"},{"id":52892,"name":"buffalo bayou"}]}
"""

struct PagedBreweries : Codable {
struct Meta : Codable {
let page : Int
let totalPages:Int
let perPage:Int
let totalRecords: Int
}

struct Brewery : Codable {
let id:Int
let name:String

}
let meta:Meta
let breweries :[Brewery]
}

func jsonNested(){
let jsonData = Data(jsonNestedString.utf8)
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let data = try! decoder.decode(PagedBreweries.self, from: jsonData)
print(data)
}

jsonNested()

// Printed result:
// PagedBreweries(meta: __lldb_expr_1.PagedBreweries.Meta(page: 1, totalPages: 4, perPage: 10, totalRecords: 38), breweries: [__lldb_expr_1.PagedBreweries.Brewery(id: 1234, name: "saintArnold"), __lldb_expr_1.PagedBreweries.Brewery(id: 52892, name: "buffalo bayou")])

How I can parse JSON with alamofire

Your response is a JSON Object known as Dictionary, use following line

let itemObject = response.result.value as? [String : Any]

go ahead with parsing inner array items

if let array = itemObject?["items"] as? [[String : Any]] {
for dict in array {
guard
let id = dict["id"] as? Int,
let name = dict["name"] as? String,
let owner = dict["owner"] as? [String : Any],
let ownerId = owner["id"] as? Int

else {
print("Error parsing \(dict)")
continue
}

print(id, ownerId, name)
}
}

Instead of parsing JSON manually, use Codable with Alamofire's responseData, below is an example

struct Item: Decodable {
let id: Int
let name: String
let owner: Owner
}
struct Owner: Decodable {
let id: Int
let login: String
}
struct PageData: Decodable {
let totalCount: Int
let incompleteResults: Bool
let items: [Item]

enum CodingKeys: String, CodingKey {
case totalCount = "total_count"
case incompleteResults = "incomplete_results"
case items
}
}

Alamofire.request("URL").responseData { response in
switch response.result {
case .failure(let error):
print(error)
case .success(let data):
do {
let pageData = try JSONDecoder().decode(PageData.self, from: data)
print(pageData, pageData.items.first?.name ?? "", pageData.items.first?.owner.id ?? 0)
} catch let error {
print(error)
}
}
}

Swift 5 Parse Json data from https request

you need to do.

  1. convert responsedata to json

  2. read the formate(Here you will get dictionary) which you get.

        guard let url = URL(string: "https://sochain.com//api/v2/get_price/DOGE/USD") else { return }

    let task = URLSession.shared.dataTask(with: url) { data, response, error in

    guard let data = data, error == nil else { return }

    do {
    // make sure this JSON is in the format we expect
    // convert data to json
    if let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] {
    // try to read out a dictionary
    print(json)
    if let data = json["data"] as? [String:Any] {
    print(data)
    if let prices = data["prices"] as? [[String:Any]] {
    print(prices)
    let dict = prices[0]
    print(dict)
    if let price = dict["price"] as? String{
    print(price)
    }
    }
    }
    }
    } catch let error as NSError {
    print("Failed to load: \(error.localizedDescription)")
    }

    }

    task.resume()


Related Topics



Leave a reply



Submit