<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">Given that, per his description, “metadata” can be anything, creating a struct doesn’t really help.<div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Jon Shier</div><div class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Jun 18, 2017, at 9:00 PM, somu subscribe <<a href="mailto:somu.subscribe@gmail.com" class="">somu.subscribe@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Create a <font face="Menlo" color="#0433ff" class="">struct</font> for Metadata and conform to <font color="#0433ff" face="Menlo" class="">Coding</font></div><div class=""><br class=""></div><div class=""><b class="">Code:</b></div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">let</span> string = <span style="color: #d12f1b" class="">"""</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27); background-color: rgb(255, 255, 255);" class="">{</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27); background-color: rgb(255, 255, 255);" class=""> "id": "4yq6txdpfadhbaqnwp3",</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27); background-color: rgb(255, 255, 255);" class=""> "email": "<a href="mailto:john.doe@example.com" class="">john.doe@example.com</a>",</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27); background-color: rgb(255, 255, 255);" class=""> "name":"John Doe",</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27); background-color: rgb(255, 255, 255);" class=""> "metadata": {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27); background-color: rgb(255, 255, 255);" class=""> "link_id": "linked-id",</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27); background-color: rgb(255, 255, 255);" class=""> "buy_count": 4</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27); background-color: rgb(255, 255, 255);" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27); background-color: rgb(255, 255, 255);" class="">}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27); background-color: rgb(255, 255, 255);" class="">"""</div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">let</span><span style="" class=""> data = </span><span style="color: #4f8187" class="">string</span><span style="" class="">.</span><span style="color: #3e1e81" class="">data</span><span style="" class="">(using: .</span><span style="color: #703daa" class="">utf8</span><span style="" class="">)! </span>//Force unwrapping just for ease of explanation, use guard instead</div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">struct</span> User: <span style="color: #703daa" class="">Codable</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class=""> <br class="webkit-block-placeholder"></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);" class=""><span style="" class=""> </span>//Struct to handle the nested JSON</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #ba2da2" class="">struct</span> Metadata : <span style="color: #703daa" class="">Codable</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #ba2da2" class="">var</span> linkID : <span style="color: #703daa" class="">String</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #ba2da2" class="">var</span> buyCount : <span style="color: #703daa" class="">Int</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class=""> <br class="webkit-block-placeholder"></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);" class=""><span style="" class=""> </span>//Customisation, if you want if you want your properties to be different from the JSON key</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #ba2da2" class="">private</span> <span style="color: #ba2da2" class="">enum</span> CodingKeys : <span style="color: #703daa" class="">String</span>, <span style="color: #703daa" class="">CodingKey</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #ba2da2" class="">case</span> linkID = <span style="color: #d12f1b" class="">"link_id"</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #ba2da2" class="">case</span> buyCount = <span style="color: #d12f1b" class="">"buy_count"</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 13px;" class=""> <br class="webkit-block-placeholder"></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #ba2da2" class="">var</span> id: <span style="color: #703daa" class="">String</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #ba2da2" class="">var</span> email : <span style="color: #703daa" class="">String</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #ba2da2" class="">var</span> name : <span style="color: #703daa" class="">String</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #ba2da2" class="">var</span> metadata : <span style="color: #4f8187" class="">Metadata</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">}</div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">let</span> decoder = <span style="color: #703daa" class="">JSONDecoder</span>()</div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(186, 45, 162); background-color: rgb(255, 255, 255);" class="">do<span style="" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #ba2da2" class="">let</span> user = <span style="color: #ba2da2" class="">try</span> <span style="color: #4f8187" class="">decoder</span>.<span style="color: #3e1e81" class="">decode</span>(<span style="color: #4f8187" class="">User</span>.<span style="color: #ba2da2" class="">self</span>, from: <span style="color: #4f8187" class="">data</span>)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #3e1e81" class="">print</span>(user)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(186, 45, 162); background-color: rgb(255, 255, 255);" class="">catch<span style="" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #3e1e81" class="">print</span>(<span style="color: #d12f1b" class="">"error:</span>\<span style="color: #d12f1b" class="">(</span>error<span style="color: #d12f1b" class="">)"</span>)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">}</div></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-family: Helvetica; font-size: 12px;" class=""><b class="">Reference:</b></span></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);" class=""><br class=""></div></div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255);" class=""><a href="https://developer.apple.com/videos/play/wwdc2017/212/" class="">https://developer.apple.com/videos/play/wwdc2017/212/</a></div><div class=""><b class=""><br class=""></b></div><div class=""><br class=""></div><div class="">Regards,</div><div class="">Muthu</div><div class=""><br class=""></div><div class=""><br class=""></div><br class=""><div class=""><blockquote type="cite" class=""><div class="">On 19 Jun 2017, at 3:29 AM, Jon Shier via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>The more I use Codable, the less suited to networking it becomes. In reading a variety of blog posts about implementing custom Decodable support from JSON, I kept running across the same pattern. Basically, users had started implementing their own decoding protocols which wrap Decodable types, and have a type that represents the JSON representation and then their real type, with an initializer connecting the two. But apparently this is Apple’s official solution, which is rather terrible since it would be completely unnecessary if the Decodable APIs were more flexible or we could access keys by key path rather than nesting full containers. I can’t image how much code I would have to add to decode the nasty JSON APIs I’ve used Argo to parse before. Every type would need an underlying raw representation that, at the very least, would need custom keys, lest I pollute even those models with the terrible keys the JSON actually has. Not to mention the various transforms I needed. Once I hit any reasonably complex API, Argo is far far simpler to implement in fewer lines of code.<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>In trying to make Argo’s JSON enum Decodable itself, I can’t seem to find a way to access the Any representation of the raw JSON. In fact, it appears there’s no way to represent an Any value in Codable at all, which makes Codable rather useless for the scenarios like the one that prompted this thread. Without such an ability it’s impossible to actually use Codable with all of the JSON out there, where other solutions work just fine. Argo’s JSON type is decodable by Argo, so you can use it to represent a blob of JSON just fine. Other existing JSON frameworks have similar solutions. <br class=""><br class=""><br class=""><br class="">Jon<br class=""><br class=""><blockquote type="cite" class="">On Jun 18, 2017, at 3:21 AM, Rien via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:<br class=""><br class="">Dang, hit send too soon. Sorry.<br class=""><br class="">This does not address your question, so please ignore… (foot in mouth)!<br class=""><br class="">Regards,<br class="">Rien<br class=""><br class="">Site: <a href="http://balancingrock.nl/" class="">http://balancingrock.nl</a><br class="">Blog: <a href="http://swiftrien.blogspot.com/" class="">http://swiftrien.blogspot.com</a><br class="">Github: <a href="http://github.com/Balancingrock" class="">http://github.com/Balancingrock</a><br class="">Project: <a href="http://swiftfire.nl/" class="">http://swiftfire.nl</a> - An HTTP(S) web server framework in Swift<br class=""><br class=""><br class=""><br class=""><br class=""><br class=""><br class=""><br class=""><blockquote type="cite" class="">On 18 Jun 2017, at 09:19, Rien <<a href="mailto:Rien@Balancingrock.nl" class="">Rien@Balancingrock.nl</a>> wrote:<br class=""><br class="">Are you looking for a general purpose JSON interpreter / API ?<br class=""><br class="">There are many of them around, and in fact I do have my own: <a href="https://github.com/Balancingrock/VJson" class="">https://github.com/Balancingrock/VJson</a><br class=""><br class="">With VJson I would write:<br class=""><br class="">let json = VJson.parse(… your json object…)<br class=""><br class="">and then access the metadata as:<br class=""><br class="">let buyCount = (json | ”metadata” | ”buy_count”)?.intValue<br class=""><br class="">or:<br class=""><br class="">var buyCount: Int &= json | “metadata” | “buy_count”<br class=""><br class="">To loop over the content of metadata:<br class=""><br class="">for item in (json | “metadata”) ?? [ ] {<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>print (item.nameValue)<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>switch item.type {<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>case .object: …<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>case .number: …<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>case .string: …<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>etc...<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}<br class="">}<br class=""><br class="">Obviously I am plugging my own code here, but there are many others around, I understand that SwiftyJSON is quite popular but I have not used that myself.<br class=""><br class="">Regards,<br class="">Rien<br class=""><br class="">Site: <a href="http://balancingrock.nl/" class="">http://balancingrock.nl</a><br class="">Blog: <a href="http://swiftrien.blogspot.com/" class="">http://swiftrien.blogspot.com</a><br class="">Github: <a href="http://github.com/Balancingrock" class="">http://github.com/Balancingrock</a><br class="">Project: <a href="http://swiftfire.nl/" class="">http://swiftfire.nl</a> - An HTTP(S) web server framework in Swift<br class=""><br class=""><br class=""><br class=""><br class=""><br class=""><br class=""><br class=""><blockquote type="cite" class="">On 18 Jun 2017, at 04:07, Chris Anderson via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:<br class=""><br class="">Say I have a JSON object such as:<br class=""><br class="">{<br class=""> "id": "4yq6txdpfadhbaqnwp3",<br class=""> "email": "<a href="mailto:john.doe@example.com" class="">john.doe@example.com</a>",<br class=""> "name":"John Doe",<br class=""> "metadata": {<br class=""> "link_id": "linked-id",<br class=""> "buy_count": 4<br class=""> }<br class="">}<br class=""><br class="">And with a struct of:<br class=""><br class="">struct User: Codable {<br class="">var id: String<br class="">var email: String<br class="">var name: String<br class="">}<br class=""><br class="">How can I decode the `metadata` field into a Dictionary?<br class=""><br class="">I’ve tried doing things such as, in my struct,<br class=""><br class="">var metadata: Dictionary<br class=""><br class="">or<br class=""><br class="">var metadata: [String: Any]<br class=""><br class="">But I get the error <br class=""><br class="">MyPlayground.playground:3:7: note: cannot automatically synthesize 'Encodable' because '<<error type>>' does not conform to 'Encodable'<br class="">var metadata: Dictionary <br class=""><br class="">A meta or metadata field on many APIs (such as <a href="http://www.stripe.com/" class="">www.stripe.com</a>) can contain whatever you want, and I still want to be able to process it on the Swift end. How can I store that meta data field into a Dictionary that I can parse apart manually after?<br class=""><br class="">Thanks!<br class=""><br class="">Chris Anderson<br class=""><br class=""><span class="Apple-tab-span" style="white-space:pre">        </span><br class=""><br class=""><br class=""><br class="">_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-users" class="">https://lists.swift.org/mailman/listinfo/swift-users</a><br class=""></blockquote><br class=""></blockquote><br class="">_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-users" class="">https://lists.swift.org/mailman/listinfo/swift-users</a><br class=""></blockquote><br class="">_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-users" class="">https://lists.swift.org/mailman/listinfo/swift-users</a><br class=""></div></div></blockquote></div><br class=""></div></div></blockquote></div><br class=""></div></body></html>