swift 4 decodable Parsing array value for index 0

Using SWIFT4 and Decodable I am trying to parse the first result 26537 from the array

pageids
at this JSON URL, see JSON IMAGE with the following code but I get nil value everytime; the issue seems to be with the fact that I can't assign any variable to represent pageids[0] ? How ? any suggestion ? How do you get the ID number in pageids ?


CODE

struct WikiData: Decodable { 
var batchcomplete: String 
var query: [Query] 
var pageids : [Pageids] 
} 

struct Query : Decodable {
var pageids : [Pageids] 
} 

struct Pageids : Decodable {
// need first item of the array as seen in JSON below
} 

let jsonData = try! Data(contentsOf: url) 
let datastring = String(data: jsonData, encoding: .utf8) 
print (datastring as Any) / 

let id = try? JSONDecoder().decode(WikiData.self, from: jsonData) 
print(WikiData?.pageids as Any)


JSON EXTRACT

{"batchcomplete":"","query":{"pageids":["26537"],"pages":{"26537":{"pageid":26537,"ns":0,"title":"Rose","extract":"A rose"}}}}

Well, several things:


1. Don't use "try?". All that does is hide error information if an error occurs. Instead, use a proper do/catch construct, and print the error that's passed into the catch block. When JSONDecoder throws an error, the message includes information about exactly where in the data the failure occurred.


2. What is "WikiData?.pageids" supposed to mean? This line isn't valid syntax. Perhaps you meant:


     print(id?.pageids as Any)


instead? That at least makes sense in terms of the problem you're asking about.


3. Is your "Pageids" structure really empty of members, or is there an actual declaration at line 12? I would expect the decode to fail if there are no members.


It looks like you think "Pageids" represents multiple page IDs, but that's not what your code says. The "pageids" property of WikiData is itself an array, so Pageids is the structure of a single page ID, which (from your JSON extract) looks like it's a string. If that's correct, you need either this:


struct Pageid : Decodable { // struct name should be singular, not plural
     var pageID: String
}


or, simpler, this:


struct WikiData: Decodable {  
     var batchcomplete: String  
     var query: [Query]  
     var pageids : [String]  
}
swift 4 decodable Parsing array value for index 0
 
 
Q