Poison.decode! - (MatchError) no match of right hand side value

(Please note: Exlir/Poison Experience Level: Beginner so please Eli5)

Given:

iex(1)> results = HTTPoison.get!("https://nodes.wavesnodes.com/assets/balance/3PDh3tDWWXmJtfoqRkf1p9PnZAHmBLcWXFW")
%HTTPoison.Response{
  body: "{\"address\":\"3PDh3tDWWXmJtfoqRkf1p9PnZAHmBLcWXFW\",\"balances\":[{\"assetId\":\"6GQkwHGyd769EghKQ6qXhAHG55JyaFjK7VGWEKSSg4Z8\",\"balance\":100000000,\"reissuable\":false,\"minSponsoredAssetFee\":null,\"sponsorBalance\":null,\"quantity\":2099443248805633,\"issueTransaction\":{\"senderPublicKey\":\"2TW1p199hYspTxt3hZc2wm9VGGDE4qFxyY2Th1HoFVww\",\"quantity\":2100000000000000,\"fee\":100000000,\"description\":\"\\\"Progression\\\" conception intelligente de jeton innovant qui se limite à évaluer 0,0001 de vagues par jour, les commandes sont limitées à 100 minutes pour stimuler la progression quotidienne.\\n\\\"Progression\\\" innovative token intelligent design that is limited to valuing 0.0001 of waves per day, orders are limited to 100 minutes to stimulate daily progression.\",\"type\":3,\"version\":2,\"reissuable\":false,\"script\":\"base64:AQQAAAALc3RhcnRIZWlnaHQAAAAAAAAVWMoEAAAACnN0YXJ0UHJpY2UAAAAAAAAAJxAEAAAACGludGVydmFsCQAAaAAAAAIAAAAAAAAAABgAAAAAAAAAADwEAAAAA2V4cAkAAGgAAAACCQAAaAAAAAIAAAAAAAAAAGQAAAAAAAAAADwAAAAAAAAAA+gEAAAAByRtYXRjaDAFAAAAAnR4AwkAAAEAAAACBQAAAAckbWF0Y2gwAgAAABNFeGNoYW5nZVRyYW5zYWN0aW9uBAAAAAFlBQAAAAckbWF0Y2gwBAAAAARkYXlzCQAAaQAAAAIJAABlAAAAAgUAAAAGaGVpZ2h0BQAAAAtzdGFydEhlaWdodAUAAAAIaW50ZXJ2YWwDAwMJAABnAAAAAggFAAAAAWUAAAAFcHJpY2UJAABoAAAAAgUAAAAKc3RhcnRQcmljZQkAAGQAAAACAAAAAAAAAAABCQAAaAAAAAIFAAAABGRheXMFAAAABGRheXMJAQAAAAEhAAAAAQkBAAAACWlzRGVmaW5lZAAAAAEICAgFAAAAAWUAAAAJc2VsbE9yZGVyAAAACWFzc2V0UGFpcgAAAApwcmljZUFzc2V0BwkAAGcAAAACBQAAAANleHAJAABlAAAAAggIBQAAAAFlAAAACXNlbGxPcmRlcgAAAApleHBpcmF0aW9uCAgFAAAAAWUAAAAJc2VsbE9yZGVyAAAACXRpbWVzdGFtcAcJAABnAAAAAgUAAAADZXhwCQAAZQAAAAIICAUAAAABZQAAAAhidXlPcmRlcgAAAApleHBpcmF0aW9uCAgFAAAAAWUAAAAIYnV5T3JkZXIAAAAJdGltZXN0YW1wBwMJAAABAAAAAgUAAAAHJG1hdGNoMAIAAAAPQnVyblRyYW5zYWN0aW9uBAAAAAJ0eAUAAAAHJG1hdGNoMAYGF8/Q+A==\",\"sender\":\"3PMQ27yBjrshdxGmFjTZHY6taN17saQyfM8\",\"feeAssetId\":null,\"chainId\":87,\"proofs\":[\"2YAgh7QQ49oFqgTfDYT5mh58xcqspR2kQJrpiLYRTtyPrtakUuYjVyt9ejr5m3suF7FZUhze1ZmXU1Ry6yJdWheV\"],\"assetId\":\"6GQkwHGyd769EghKQ6qXhAHG55JyaFjK7VGWEKSSg4Z8\",\"decimals\":8,\"name\":\"Progression\",\"id\":\"6GQkwHGyd769EghKQ6qXhAHG55JyaFjK7VGWEKSSg4Z8\",\"timestamp\":1550319832173}},{\"assetId\":\"51LxAtwBXapvvTFSbbh4nLyWFxH6x8ocfNvrXxbTChze\",\"balance\":6,\"reissuable\":false,\"minSponsoredAssetFee\":10000,\"sponsorBalance\":90323001,\"quantity\":9999999886689962,\"issueTransaction\":{\"senderPublicKey\":\"FCfKDQiCaNruTAj984WCrQLHS1j1c8Fv5JzgKswgAhoy\",\"quantity\":10000000000000000,\"fee\":100000000,\"description\":\"TOR Corporation\\nОфициальная чат-группа монет TOR https://t.me/projecttorcoin \\nWhite paper https://docs.google.com/document/d/1LJfo6X82cAjLI4tahkVN-jR_GU8a4d2aZ_tFtXP4yYI/edit?usp=sharing Дорожная карта https://docs.google.com/document/d/1qBheIvDO3KSfyQnWQdbwUBKZtalaILhCak03fJlQRTk/edit?usp=sharing\",\"type\":3,\"version\":2,\"reissuable\":false,\"script\":null,\"sender\":\"3P287ZEpVU14SAr4jhCF58fZPzfkQv4LXqm\",\"feeAssetId\":null,\"chainId\":87,\"proofs\":[\"4f7ZzQ5ZgZskxnCDhU4RKx6MWCHNVrnJs8hKRfGWX35aSyJtcJqrQuUaahrqcAnYU8dpDq8rRUvTtwa6VhcEEx7v\"],\"assetId\":\"51LxAtwBXapvvTFSbbh4nLyWFxH6x8ocfNvrXxbTChze\",\"decimals\":6,\"name\":\"TORCorp\",\"id\":\"51LxAtwBXapvvTFSbbh4nLyWFxH6x8ocfNvrXxbTChze\",\"timestamp\":1541398865252}},{\"assetId\":\"GAAZu8fsWfrZBFgweMMAFEcjrW9CFRxPEqmGcbSGWVDF\",\"balance\":300000000,\"reissuable\":false,\"minSponsoredAssetFee\":null,\"sponsorBalance\":null,\"quantity\":4999999200000000,\"issueTransaction\":{\"senderPublicKey\":\"jYTtcsLgDD5NcoDHUAnHLdC9PjeSwyjxFysptotgHjq\",\"quantity\":10000000000000000,\"fee\":100000000,\"description\":\"K975 Token\",\"type\":3,\"version\":2,\"reissuable\":false,\"script\":null,\"sender\":\"3PMSXKE9AqLMw2Yh5nTscm2DSJP677hf2QJ\",\"feeAssetId\":null,\"chainId\":87,\"proofs\":[\"2GPLuTFZFb8bLKV1AJ15VUtjHpmMcvXo8nNmi9F5pwdptgudiGXwU2efM5BFCP2FvK7zAdR5zQPBguJFY4kyUqXd\"],\"assetId\":\"GAAZu8fsWfrZBFgweMMAFEcjrW9CFRxPEqmGcbSGWVDF\",\"decimals\":8,\"name\":\"K975\",\"id\":\"GAAZu8fsWfrZBFgweMMAFEcjrW9CFRxPEqmGcbSGWVDF\",\"timestamp\":1568118516242}},{\"assetId\":\"2rdZt8RxEftbmz34HLBd7Au2icyScgAe5YoipeT7GwK2\",\"balance\":1,\"reissuable\":false,\"minSponsoredAssetFee\":null,\"sponsorBalance\":null,\"quantity\":49999986,\"issueTransaction\":{\"senderPublicKey\":\"StYAY98BDsSDvhMRFauKxn5pWEcD6KxnRnvPDbtceZ" <> ...,
  headers: [
    {"Date", "Sat, 12 Oct 2019 20:01:32 GMT"},
    {"Content-Type", "application/json"}, 
    {"Transfer-Encoding", "chunked"},
    {"Connection", "keep-alive"},
    {"Set-Cookie",
     "__cfduid=d7522b5d559f3952392299b4e3c07a2cb1570910492; expires=Sun, 11-Oct-20 20:01:32 GMT; path=/; domain=.wavesnodes.com; HttpOnly"}, 
    {"Vary", "Accept-Encoding"},
    {"Access-Control-Allow-Origin", "*"},
    {"Access-Control-Allow-Credentials", "true"},
    {"Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, DELETE"},
    {"Access-Control-Allow-Headers",
     "DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,signature,timestamp"},
    {"Access-Control-Expose-Headers", "Content-Length,Content-Range"},
    {"Backend", "ca-2"},
    {"Content-Security-Policy",
     "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' online.swagger.io"},
    {"X-Frame-Options", "DENY"},
    {"X-XSS-Protection", "0"},
    {"X-Content-Type-Options", "nosniff"},
    {"Referrer-Policy", "unsafe-url"},
    {"CF-Cache-Status", "DYNAMIC"},
    {"Set-Cookie",
     "AWSALB=3/o7aRqBwecPhGPjflqgWoA5R8nviB4AuiJ+hzmjke8QIkv4rXRCeAGXxGHYs4OTYS+C/EfEUc83sWOSdFdpDe7ugPrSkX3GqeX4+BBNCnpy510HP/sehtLLBkim; Expires=Sat, 19 Oct 2019 20:01:32 GMT; Path=/"},
    {"Set-Cookie",
     "__cflb=3256034178; path=/; expires=Sun, 13-Oct-19 19:01:32 GMT; HttpOnly"},
    {"Expect-CT",
     "max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\""},
    {"Server", "cloudflare"},
    {"CF-RAY", "524baa8f68e9c7c5-DEN"}
  ],
  request: %HTTPoison.Request{
    body: "",
    headers: [], 
    method: :get,
    options: [],
    params: %{},
    url: "https://nodes.wavesnodes.com/assets/balance/3PDh3tDWWXmJtfoqRkf1p9PnZAHmBLcWXFW"
  },
  request_url: "https://nodes.wavesnodes.com/assets/balance/3PDh3tDWWXmJtfoqRkf1p9PnZAHmBLcWXFW",
  status_code: 200
}

Why does this fail?

iex(2)> %{data: items} = Poison.decode!(results.body, keys: :atoms)
** (MatchError) no match of right hand side value: %{address: "3PDh3tDWWXmJtfoqRkf1p9PnZAHmBLcWXFW", balances: [%{assetId: "6GQkwHGyd769EghKQ6qXhAHG55JyaFjK7VGWEKSSg4Z8", balance: 100000000, issueTransaction: %{assetId: "6GQkwHGyd769EghKQ6qXhAHG55JyaFjK7VGWEKSSg4Z8", chainId: 87, decimals: 8, description: "\"Progression\" conception intelligente de jeton innovant qui se limite à évaluer 0,0001 de vagues par jour, les commandes sont limitées à 100 minutes pour stimuler la progression quotidienne.\n\"Progression\" innovative token intelligent design that is limited to valuing 0.0001 of waves per day, orders are limited to 100 minutes to stimulate daily progression.", fee: 100000000, feeAssetId: nil, id: "6GQkwHGyd769EghKQ6qXhAHG55JyaFjK7VGWEKSSg4Z8", name: "Progression", proofs: ["2YAgh7QQ49oFqgTfDYT5mh58xcqspR2kQJrpiLYRTtyPrtakUuYjVyt9ejr5m3suF7FZUhze1ZmXU1Ry6yJdWheV"], quantity: 2100000000000000, reissuable: false, script: "base64:AQQAAAALc3RhcnRIZWlnaHQAAAAAAAAVWMoEAAAACnN0YXJ0UHJpY2UAAAAAAAAAJxAEAAAACGludGVydmFsCQAAaAAAAAIAAAAAAAAAABgAAAAAAAAAADwEAAAAA2V4cAkAAGgAAAACCQAAaAAAAAIAAAAAAAAAAGQAAAAAAAAAADwAAAAAAAAAA+gEAAAAByRtYXRjaDAFAAAAAnR4AwkAAAEAAAACBQAAAAckbWF0Y2gwAgAAABNFeGNoYW5nZVRyYW5zYWN0aW9uBAAAAAFlBQAAAAckbWF0Y2gwBAAAAARkYXlzCQAAaQAAAAIJAABlAAAAAgUAAAAGaGVpZ2h0BQAAAAtzdGFydEhlaWdodAUAAAAIaW50ZXJ2YWwDAwMJAABnAAAAAggFAAAAAWUAAAAFcHJpY2UJAABoAAAAAgUAAAAKc3RhcnRQcmljZQkAAGQAAAACAAAAAAAAAAABCQAAaAAAAAIFAAAABGRheXMFAAAABGRheXMJAQAAAAEhAAAAAQkBAAAACWlzRGVmaW5lZAAAAAEICAgFAAAAAWUAAAAJc2VsbE9yZGVyAAAACWFzc2V0UGFpcgAAAApwcmljZUFzc2V0BwkAAGcAAAACBQAAAANleHAJAABlAAAAAggIBQAAAAFlAAAACXNlbGxPcmRlcgAAAApleHBpcmF0aW9uCAgFAAAAAWUAAAAJc2VsbE9yZGVyAAAACXRpbWVzdGFtcAcJAABnAAAAAgUAAAADZXhwCQAAZQAAAAIICAUAAAABZQAAAAhidXlPcmRlcgAAAApleHBpcmF0aW9uCAgFAAAAAWUAAAAIYnV5T3JkZXIAAAAJdGltZXN0YW1wBwMJAAABAAAAAgUAAAAHJG1hdGNoMAIAAAAPQnVyblRyYW5zYWN0aW9uBAAAAAJ0eAUAAAAHJG1hdGNoMAYGF8/Q+A==", sender: "3PMQ27yBjrshdxGmFjTZHY6taN17saQyfM8", senderPublicKey: "2TW1p199hYspTxt3hZc2wm9VGGDE4qFxyY2Th1HoFVww", timestamp: 1550319832173, type: 3, version: 2}, minSponsoredAssetFee: nil, quantity: 2099443248805633, reissuable: false, sponsorBalance: nil}, }
    (stdlib) erl_eval.erl:453: :erl_eval.expr/5
    (iex) lib/iex/evaluator.ex:257: IEx.Evaluator.handle_eval/5
    (iex) lib/iex/evaluator.ex:237: IEx.Evaluator.do_eval/3
    (iex) lib/iex/evaluator.ex:215: IEx.Evaluator.eval/3
    (iex) lib/iex/evaluator.ex:103: IEx.Evaluator.loop/1
    (iex) lib/iex/evaluator.ex:27: IEx.Evaluator.init/4

Look at the output of Poison.decode!(results.body, keys: :atoms). There isn’t a data key. You’re asserting via %{data: items} that the result of Poison.decode! will be a map with an :data key and then data. The JSON does not have any such key.

2 Likes

Thanks!

Got it working with:
iex(1)> %{balances: items} = Poison.decode!(results.body, keys: :atoms)

Perhaps you should consider matching on “balances” instead of :balances