{"action":"created","issue":{"url":"https://api.github.com/repos/thoughtbot/Argo/issues/483","repository_url":"https://api.github.com/repos/thoughtbot/Argo","labels_url":"https://api.github.com/repos/thoughtbot/Argo/issues/483/labels{/name}","comments_url":"https://api.github.com/repos/thoughtbot/Argo/issues/483/comments","events_url":"https://api.github.com/repos/thoughtbot/Argo/issues/483/events","html_url":"https://github.com/thoughtbot/Argo/issues/483","id":303294941,"number":483,"title":"Decoding the same object with different keys","user":{"login":"VillSkog","id":1673791,"avatar_url":"https://avatars0.githubusercontent.com/u/1673791?v=4","gravatar_id":"","url":"https://api.github.com/users/VillSkog","html_url":"https://github.com/VillSkog","followers_url":"https://api.github.com/users/VillSkog/followers","following_url":"https://api.github.com/users/VillSkog/following{/other_user}","gists_url":"https://api.github.com/users/VillSkog/gists{/gist_id}","starred_url":"https://api.github.com/users/VillSkog/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/VillSkog/subscriptions","organizations_url":"https://api.github.com/users/VillSkog/orgs","repos_url":"https://api.github.com/users/VillSkog/repos","events_url":"https://api.github.com/users/VillSkog/events{/privacy}","received_events_url":"https://api.github.com/users/VillSkog/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-03-07T23:11:17Z","updated_at":"2018-03-08T15:16:56Z","closed_at":null,"author_association":"NONE","body":"I am currently working with an API that gives me exactly the same JSON on several endpoints (either as part of a bigger object, an array, ...) but they don't always have the same keys ...\r\nI asked the backend developer to make those uniform but he told me that the Android app already uses the API so it won't happen anytime soon.\r\n\r\nThus, I was wondering if there was any way to decode the exact same json (+/- some optionals) but with different keys, without making a whole new class ..\r\n\r\nJust to make sure I'm clear, here's an exemple:\r\n\r\njson A:\r\n\r\n```\r\n{\r\n    \"name\" : \"Awesome shop\",\r\n    \"id\" : \"42\"\r\n}\r\n```\r\njson B\r\n\r\n```\r\n{\r\n    \"shop_name\" : \"Awesome shop\",\r\n    \"shop_id\" : \"42\"\r\n}\r\n```\r\n\r\nAs of now, I use a bridge class ShopB (subclass of Shop) to decode jsonB into a Shop model. Also note that the keys are not always in the form \"var\" / \"shop_var\" like in my exemple, they are just completely unrelated."},"comment":{"url":"https://api.github.com/repos/thoughtbot/Argo/issues/comments/371517688","html_url":"https://github.com/thoughtbot/Argo/issues/483#issuecomment-371517688","issue_url":"https://api.github.com/repos/thoughtbot/Argo/issues/483","id":371517688,"user":{"login":"gfontenot","id":124499,"avatar_url":"https://avatars3.githubusercontent.com/u/124499?v=4","gravatar_id":"","url":"https://api.github.com/users/gfontenot","html_url":"https://github.com/gfontenot","followers_url":"https://api.github.com/users/gfontenot/followers","following_url":"https://api.github.com/users/gfontenot/following{/other_user}","gists_url":"https://api.github.com/users/gfontenot/gists{/gist_id}","starred_url":"https://api.github.com/users/gfontenot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gfontenot/subscriptions","organizations_url":"https://api.github.com/users/gfontenot/orgs","repos_url":"https://api.github.com/users/gfontenot/repos","events_url":"https://api.github.com/users/gfontenot/events{/privacy}","received_events_url":"https://api.github.com/users/gfontenot/received_events","type":"User","site_admin":false},"created_at":"2018-03-08T15:16:56Z","updated_at":"2018-03-08T15:16:56Z","author_association":"COLLABORATOR","body":"Yep, it's actually pretty easy with Argo. We supply an Alternative operator (`<|>`) for exactly this use case:\r\n\r\n```swift\r\nstruct Shop {\r\n  let id: String\r\n  let name: String\r\n}\r\n\r\nextension Shop: Argo.Decodable {\r\n  static func decode(_ j: JSON) -> Decoded<Shop> {\r\n    return curry(Shop.init)\r\n      <^> j <| \"id\"\r\n      <*> (j <| \"name\") <|> (j <| \"shop_name\")\r\n  }\r\n}\r\n```"}}
{"actor":{"display_login":"gfontenot"}}