0

I need to retrieve data deep within a nested JSON but I've had alot of trouble doing so. The file in question can be found at https://waterservices.usgs.gov/nwis/iv/?format=json&indent=on&sites=08155200&parameterCd=00065&siteStatus=all.

// MARK: - Post
struct Post: Codable {
    let name, declaredType, scope: String?
    let value: PostValue?
    let postNil, globalScope, typeSubstituted: Bool?

    enum CodingKeys: String, CodingKey {
        case name, declaredType, scope, value
        case postNil
        case globalScope, typeSubstituted
    }
}

// MARK: - PostValue
struct PostValue: Codable {
    let queryInfo: QueryInfo?
    let timeSeries: [TimeSery]?
}

// MARK: - QueryInfo
struct QueryInfo: Codable {
    let queryURL: String?
    let criteria: Criteria?
    let note: [Note]?
}

// MARK: - Criteria
struct Criteria: Codable {
    let locationParam, variableParam: String?
    let parameter: [JSONAny]?
}

// MARK: - Note
struct Note: Codable {
    let value, title: String?
}

// MARK: - TimeSery
struct TimeSery: Codable {
    let sourceInfo: SourceInfo?
//    let variable: Variable?
//    let values: [TimeSeryValue]?
    let name: String?
}

// MARK: - SourceInfo
struct SourceInfo: Codable {
    let siteName: String?
//    let siteCode: [SiteCode]?
//    let timeZoneInfo: TimeZoneInfo?
    let geoLocation: GeoLocation?
    let note, siteType: [JSONAny]?
//    let siteProperty: [SiteProperty]?
}

// MARK: - GeoLocation
struct GeoLocation: Codable {
    let geogLocation: GeogLocation?
    let localSiteXY: [JSONAny]?
}

// MARK: - GeogLocation
struct GeogLocation: Codable {
    let srs: String?
    let latitude, longitude: Double?
}

The code to retrieve the data:

URLSession.shared.dataTask(with: url) { data, _, _ in
    if let data = data {
        let posts = try! JSONDecoder().decode(Post.self, from: data)
        if let coord = (posts.value?.timeSeries?.sourceInfo?.geoLocation?.geogLocation?.srs) {
            print(coord)
        }
    }
}.resume()

Unfortunately, this returns with the error

error: ParseJSON.playground:330:89: error: type of expression is ambiguous without more context

1 Answer 1

1

timeSeries is defined as [TimeSery], meaning it's an array, but you're trying to access it as if it's just a single value. Since I'm not sure what your intent was, it's hard to say what the exact fix is, but one possibility is accessing the first value from it (the equivalent of asking for [0], but it returns an Optional):

posts.value?.timeSeries?.first?.sourceInfo?.geoLocation?.geogLocation?.srs

A way to debug this issue, by the way, is to break down the expression into less-complex parts (I started with just posts.value and add code back until you find the issue (in this case, timeSeries.sourceInfo)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the response! I've debugged it and it works with posts.value?.timeSeries but if I enter posts.value?.timeSeries?.sourceInfo? it gives the error Type of expression is ambiguous without more context.
Yes -- check out my answer -- timeSeries is an array. You can't access sourceInfo on it directly because you need to tell it which element you want. This is why I suggested maybe using .first. But, I don't know which element you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.