Provide subscripts for JSON.Object to return the first key-value pair whose key matches a given key#94
Conversation
|
i don’t think this is the right layer for this API. i believe there is a type |
|
I parse JSON provided by a third-party that doesn't provide a spec. I map various keys from the original JSON to new key names, and I sort the JSON based on the new key names. I've never seen duplicate keys in the provided JSON, but, if duplicate keys exist, I want to map all of them & retain all duplicate keys. I use My internal processing must extract a few values from the input JSON. The subscripts in this PR extract the first value listed in the JSON for a given key. Does swift-json provide any better way to do what I need? Here's an example of code that uses the subscripts from this PR: extension SearchResult: JSONDecodable {
fileprivate init(json: JSON.Node) throws {
guard case let .object(object) = json else {
throw MASError.jsonParsing(input: String(describing: json))
}
adamID = try object["trackId"]?.decode() ?? 0
appStorePageURL = try object["trackViewUrl"]?.decode() ?? ""
sellerURL = try object["sellerUrl"]?.decode()
self.json = String(describing: json.mappingKeys)
}
}
private extension JSON.Node {
var mappingKeys: Self {
// map keys & sort them
}
} |
|
@tayloraswift Is the above use case sufficient? |
7db2914 to
990d135
Compare
|
Thanks for resolving all my other submitted issues. If you don't want to support this in swift-json, I can close this PR & my related issue. If you would be willing to merge this, I'll rebase on main & resubmit. Thanks. My reason for wanting this is: I parse & process JSON provided by a third-party that doesn't provide a spec. I've never seen duplicate keys in the input JSON, but, if it ever contains duplicate keys, I want to pass all of them through from the input to the output, instead of only passing through one of the duplicates & its associated value. I use My JSON processing must extract a few values from the input JSON. The subscripts in this PR extract the first value listed in the JSON for a given key, which suffices for my logic. Does swift-json provide any better way to do what I need? Thanks again for the library & the new release. |
…ir whose key matches a given key. Signed-off-by: Ross Goldberg <484615+rgoldberg@users.noreply.github.com>
990d135 to
e66dba5
Compare
Provide subscripts for
JSON.Objectto return the first key-value pair whose key matches a given key.Resolve #93