Access Nested Data JSON - Closed

Hi There im triyng to decode the following json to a Type:Response, but i dont know what im doing wrong.

can anyone point me out to the solution.

Help would be much appriciated.


let json = """
[
{
"status_message": "Success",
"current_carrier": {
"network_code": "US-FIXED",
}
]
""".data(using:.utf8)!
struct Response:Codable{

  var status_message:String
  var current_carrier:CurrentCarrier

  struct  CurrentCarrier:Codable{
  var network_code:String

  }
}
let decoder = JSONDecoder()
let response = try decoder.decode([Response].self, from: json)
for i in response {
     print(i.status_message)
      print(i.current_carrier.network_code)
}

Accepted Reply

Found the solution:


Hope it helps other pepole.


let json = """
[
{
"status_message": "Success",
"current_carrier": {
"network_code": "US-FIXED",
}
]
""".data(using:.utf8)!



struct Response:Codable{
var status_message:String
var current_carrier:CurrentCarrier
}


struct  CurrentCarrier:Codable{
var network_code:String
}

let decoder = JSONDecoder() 
let response = try decoder.decode([Response].self, from: json) 
for i in response { 
    print(i.status_message) 
     print(i.current_carrier.network_code) 
}

Replies

It would help if you simplified the code to highlight your primary concern. It seems that lots of these properties are peers, and thus would be handled the same way. If you delete all of the peers except one, your code snippet will be much smaller and folks would be more likely to tackle your question.

It would also help if you posted details about what’s actually failing. I presume that

decoder.decode(_:from:)
is throwing an error. What is that error?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hi There Eskimo,


Thank you for pointing out those issues, i simplified the code, and the error im getting is indeed in the decode part, the error showen in Playground is as follows:

deferredToDate

base64

throw

[:]



The problem im facing is that im using an API that returns a url response, somtimes the json format changes and a field is added/removed, and the decode proccess fails.

Im trying to figure out what is the best way to iterate a json request? and to make the code above work.


Thank you for your time.

the error showen in Playground is as follows:

Hmmmm, something seems to have gone missing here.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Yep Thats what im trying to figure out

Found the solution:


Hope it helps other pepole.


let json = """
[
{
"status_message": "Success",
"current_carrier": {
"network_code": "US-FIXED",
}
]
""".data(using:.utf8)!



struct Response:Codable{
var status_message:String
var current_carrier:CurrentCarrier
}


struct  CurrentCarrier:Codable{
var network_code:String
}

let decoder = JSONDecoder() 
let response = try decoder.decode([Response].self, from: json) 
for i in response { 
    print(i.status_message) 
     print(i.current_carrier.network_code) 
}

For your information on how to use the forum: marking a thread as closed means clicking on "Correct answer" on the answer that was the correct solution.


With 2 benefits:

- future readers can rapidly find what was the correct answer (not easy otherwise when there has been a lot of discussion)

- thread appears with a green label: we know there is no need to go and look to provide an answer


You can mark your own reply as correct, if you finally found the solution yourself.