Ayrad
Ayrad

Reputation: 4056

Trying to decode value in swift 4.2 using its position in the JSON structure

How can I decode the following using swift Decodable? I'm only interested in the extract value:

{  
   "batchcomplete":"",
   "query":{  
      "normalized":[  ],
      "pages":{  
         "434325":{    //can be any number!
            "pageid":434325,
            "ns":0,
            "title":"asdfasdfsa",
            "extract":""

I'm attempting the following:

struct Entry: Decodable {
    let batchcomplete: String
    let query: Query
    
    struct Query: Decodable {
        let normalized: [String]
        let pages: Page
        
        struct Page: Decodable {
            let pageid: Entry // I think this is going to be an issue
            
            struct Entry: Decodable {
                let title: String
                let extract: String 
            }
            
        }
    }
}

I'm trying to retrieve the extract like this:

  print(entry.query.pages.first.extract) 

Is there a way to use .first for this ?

I'm only every going to get maximum one page so ideally I would just grab the first element.

Upvotes: 0

Views: 120

Answers (1)

user28434'mstep
user28434'mstep

Reputation: 6600

Your decoding code is ok up until `Query type.

So what should be used instead:

struct Query: Decodable {
    let normalized: [String]
    let pages: [String: Page]

    struct Page: Decodable {
        let pageid: Int
        let title: String
        let extract: String
    }
}

So, key points:

  1. If you don't know what keys would be there, use [String: <SomeDecodableType>] because any JSON Object can be mapped to the [String: <Decodable>].
  2. You don't need to declare Page.Entry, just put all the fields to the Page
  3. To get extract instead of entry.query.pages.first.extract you will have to use entry.query.pages.first?.value.extract (extract of first random page, because [String: Page] is unsorted) or entry.query.pages.sorted(by: sorter).first?.value.extract (where sorter is some function, that takes two key-value pairs and returns true if first one should go before second one in order) to get first using some order.

Upvotes: 1

Related Questions