How to deserialize below JSON response in swift? - json

I have a json response. I'm bit confused on how to deserialize it. I want to extract the Id and Name from the below json response and store them as key value pairs in a dictionary.
JSON Response
("[{\"attributes\":{\"type\":\"User\",\"url\":\"/services/data/v1.0/objects/User/01234\"},\"Id\":\"01234\",\"Name\":\"User1\",\"RecordTypeId\":\"1\"},{\"attributes\":{\"type\":\"User\",\"url\":\"/services/data/v1.0/objects/User/01235\"},\"Id\":\"01235\",\"Name\":\"User2\",\"RecordTypeId\":\"2\"},{\"attributes\":{\"type\":\"User\",\"url\":\"/services/data/v1.0/objects/User/01236\"},\"Id\":\"01236\",\"Name\":\"User3\",\"RecordTypeId\":\"3\"},{\"attributes\":{\"type\":\"User\",\"url\":\"/services/data/v1.0/objects/User/01237\"},\"Id\":\"01237\",\"Name\":\"User4\",\"RecordTypeId\":\"4\"}]")
I want to extract the id and name values from above json response and store them in a dictionary like below
dict = [01234:"User1", 01235:"User2", 01236:"User3", 01237:"User4"]

The server's response seems like an array, so you could deserialize it to something like [String].
Then you define a structure that corresponds to the json inside that array, something like this:
struct ResponseElement: Codable {
struct attributes : Codable {
let type, url: String
}
let Id, Name, RecordTypeId: String
}
After that you decode it using this: JSONDecoder().decode([ResponseElement].self, ...
Finally you go over every element in your JSON to fill the dictionary, which has the following type [Int:String]
I tested this on Playgrounds, the full code is:
import Foundation
let serverResponse: [String] = [
"[{\"attributes\":{\"type\":\"User\",\"url\":\"/services/data/v1.0/objects/User/01234\"},\"Id\":\"01234\",\"Name\":\"User1\",\"RecordTypeId\":\"1\"},{\"attributes\":{\"type\":\"User\",\"url\":\"/services/data/v1.0/objects/User/01235\"},\"Id\":\"01235\",\"Name\":\"User2\",\"RecordTypeId\":\"2\"},{\"attributes\":{\"type\":\"User\",\"url\":\"/services/data/v1.0/objects/User/01236\"},\"Id\":\"01236\",\"Name\":\"User3\",\"RecordTypeId\":\"3\"},{\"attributes\":{\"type\":\"User\",\"url\":\"/services/data/v1.0/objects/User/01237\"},\"Id\":\"01237\",\"Name\":\"User4\",\"RecordTypeId\":\"4\"}]"
]
struct ResponseElement: Codable {
struct attributes : Codable {
let type, url: String
}
let Id, Name, RecordTypeId: String
}
var dict:[Int:String] = [:]
if let serverResponseAsData = serverResponse[0].data(using: .utf8),
let response = try? JSONDecoder().decode([ResponseElement].self, from: serverResponseAsData) {
response.forEach { element in
if let idAsInt = Int(element.Id) {
dict[idAsInt] = element.Name
}
}
}
print(dict)
And this prints:
[1235: "User2", 1237: "User4", 1236: "User3", 1234: "User1"]

Related

I expect the result to be a dictionary but it is accessed like a property... why?

I have this JSON coming from a server...
{
"cars": [
{
"name": "Ferrari",
"price": "100"
},
{
"name": "Lamborghini",
"price": "200"
},
{
"name": "Ford Pinto",
"price": "1"
}
]
}
This JSON is a dictionary called cars that contains an array of cars, right?
Then I have this struct...
struct Cars: Codable {
let cars: [Car]
}
struct Car: Codable, Hashable, Identifiable {
let id = UUID()
let name: String
let price: String
}
and I decode the JSON using this:
let (data, _) = try await urlSession.data(from: url)
let result = try JSONDecoder().decode(Cars.self, from: data)
let listOfCars = result.cars
This is something I don't understand.
in result.cars, cars is a property of result that was declared as an array in the struct Cars. Not a dictionary.
I was expecting to access it using result["cars"].
Why is that?
In your code here...
let (data, _) = try await urlSession.data(from: url)
let result = try JSONDecoder().decode(Cars.self, from: data)
let listOfCars = result.cars
result is an instance of the Struct Cars. Your set up code has told Swift how to translate from a JSON dictionary into your own Struct.
So everything inside of result is accessed just like how you would access it in the following...
let result = Cars(cars: [
Car(name: "Ford", price: "£10,000")
])
print(result.cars)
The only difference is how you are creating it. Instead of using the init method like this you are using a JSON decode to decode some JSON into your custom type.
As said in the comments and answers, it takes a result type according to your decode strategy. In your code result type is Cars not a dictionary. So you access the properties with using result.cars
If you want something as dictionary instead, you need to decode it like
let result = try decode.decode([String : [Car]].self, from: data)
Now you can access them like a dictionar
print(result["cars"]?.first?.name) // Optional("Ferrari")

I can't decode a JSON file in Swift, even so I could read it into a Data object

I'm quite new in Swift. I created a simple terminal app inside Xcode to learn about decoding JSON files. Even so I was able to read the file into a Data object, I couldn't decode it for my struct:
Here is my struct:
struct Person: Codable
{
var name: String
var surname: String
}
Here is my simple JSON file:
[
{
"name": "Abc",
"surname": "Def"
}
]
And here is my Swift code to decode the JSON file:
let url = URL(fileURLWithPath: "/Users/abcd/Documents/test.json")
if let data = try? Data(contentsOf: url)
{
print(data) // prints size (in bytes) the data correctly
if let person = try? JSONDecoder().decode(Person.self, from: data)
{
print(person) // did not printed
}
}
The JSON contains an array. Accordindly, you should use one in your code as well:
if let persons = try? JSONDecoder().decode([Person].self, from: data)
...

how to Converting JSON into Codable in swift 4.2?

I am using Xcode 10.1 and Swift 4.2. When i try to convert JSON response into Codable class it gives an error that Expected to decode Array<Any> but found a string/data instead.
My Actual JSON response is Like this from API .
{
"d": "[{\"Data\":{\"mcustomer\":[{\"slno\":1000000040.0,\"fstname\":null}]},\"Status\":true}]"
}
My Model is Like this
class MainData: Codable{
var d: [SubData]
}
class SubData : Codable {
var Data : Customer
var Status : Bool?
}
class Customer : Codable {
var mcustomer : [Detail]
}
class Detail : Codable {
var slno : Double?
var fstname : String?
}
And I am Decode this Model using JSONDecoder()
let decoder = JSONDecoder()
let deco = try decoder.decode(MainData.self, from: data)
but, I am unable to Decode this Json into My Model.
Your API is wrong. You array in json shouldn't have quotation marks around it. Otherwise you're declaring that value for key "d" is string
"[...]"
[...]
Suggestions:
Variables and constants should start with small capital letter. Otherwise for example your Data property would cause confusion with Data type. For renaming it while decoding you can use CodingKeys
If you don't need to encode your model, you can just implement Decodable protocol
You can use struct instead of class for your model
The top-level JSON object is a dictionary with the key "d" and a string value, representing another JSON object (sometimes called "nested JSON"). If the server API cannot be changed then the decoding must be done in two steps:
Decode the top-level dictionary.
Decode the JSON object from the string obtained in step one.
Together with Robert's advice about naming, CodingKeys and using structs it would look like this:
struct MainData: Codable {
let d: String
}
struct SubData : Codable {
let data : Customer
let status : Bool
enum CodingKeys: String, CodingKey {
case data = "Data"
case status = "Status"
}
}
struct Customer : Codable {
let mcustomer : [Detail]
}
struct Detail : Codable {
let slno : Double
let fstname : String?
}
do {
let mainData = try JSONDecoder().decode(MainData.self, from: data)
let subData = try JSONDecoder().decode([SubData].self, from: Data(mainData.d.utf8))
print(subData)
} catch {
print(error)
}
For your solution to work, the JSON reponse has to be following format
let json = """
{
"d": [
{
"Data": {
"mcustomer": [
{
"slno": 1000000040,
"fstname": null
}
]
},
"Status": true
}
]
}
"""
But, as you can see, the JSON response you are getting is quite different than you are expecting. Either you need to ask to change the response or you need to change your model.

How can i append JSON with multiple struct into array?

In order to parse the JSON, I needed to use 3 structs.
struct AppleApi: Decodable {
let feed: Feed
}
struct Feed: Decodable {
let results: [Result]
}
struct Result: Decodable {
let artistName: String
let artWorkUrl: String
enum CodingKeys : String, CodingKey {
case artistName = "artistName"
case artWorkUrl = "artworkUrl100"
}
}
But when I try to populate the array with that parsed data I got that message:
Cannot convert value of type '[Result]' to expected argument type
'AppleApi'
This is my error message:
do {
let appData = try JSONDecoder().decode(AppleApi.self, from: jsonData)
print(appData.feed.results.count)
var dataApp = appData.feed.results
print(appData)
DispatchQueue.main.async {
self.feedReseult.append(dataApp)
self.myCollectionView.reloadData()
}
} catch let err {
print("Error",err)
}
And this is my array:
var feedReseult = [AppleApi]()
I probably need to reach to 3. struct to reach the array inside JSON in order to have same type of argument type. How can I do that?
Your declaration of feedReseult should be this,
var feedReseult = [Result]()
and append the dataApp as below,
DispatchQueue.main.async {
self.feedReseult.append(contentsOf: dataApp)
self.myCollectionView.reloadData()
}
Also i feel a typo, feedResult instead of feedReseult
It looks like your struct from Json is not put together correct, you should just need one struct per one JSON load. Could you give JSON sample please?
If you want to Decode Feed which is part of AppleAPI then you should create an object that is of type AppleApi.Feed and put results into that.
Hope this helps a little

SwiftyJSON looping through an array of JSON objects

[
{
"cont": 9714494770,
"id": "1",
"name": "Kakkad"
},
{
"cont": 9714494770,
"id": "2",
"name": "Ashish"
}
]
The one above is a json array filled with JSON objects. I don't know how to parse through this with SwiftyJSON
Example from the SwiftyJSON page, adapted to your data:
let json = JSON(data: dataFromNetworking)
for (index, object) in json {
let name = object["name"].stringValue
println(name)
}
Assuming [{"id":"1", "name":"Kakkad", "cont":"9714494770"},{"id":"2", "name":"Ashish", "cont":"9714494770"}] is assigned to a property named jsonData.
let sampleJSON = JSON(data: jsonData)
let sampleArray = sampleJSON.array sampleArray is an optional array of JSON objects.
let firstDict = sampleArray[0] firstDict is an optional JSON dict.
let name = firstDict["name"] is an optional JSON object
let virtName = name.string is a optional string (In this case "Kakkad").
let realName = name.stringValue realName is a string or an empty string.
You could also use:
let longName = sampleJSON[0]["name"].stringValue
After you initialize the JSON object with data all of the elements are JSON types until you convert them to a swift type.
.string optional (string or null)
.stringValue string or "" empty
string
.dict optional ([String: AnyObject] or null)
.dictValue
([String: AnyObject] or String: AnyObject)
For Swift4 I have updated the code from Moritz answer
if let path : String = Bundle.main.path(forResource: "tiles", ofType: "json") {
if let data = NSData(contentsOfFile: path) {
let optData = try? JSON(data: data as Data)
guard let json = optData else {
return
}
//If it is a JSON array of objects
for (_, object) in json {
let name = object["name"].stringValue
print(name)
}
}
}
Swift 3 or 4 code like this:
let json = JSON(yourData)
for (_, object) in json {
let cont = object["cont"].stringValue
print(cont)
}
You can put index instead of _ if you use is anywhere in your code. If you don't use a variable, it's better to put _ (XCode also gives warnings).