I am trying to process the following JSON that I receive from an API.
{"product":"midprice",
"prices":[
["APPLE","217.88"],
["GOOGLE","1156.05"],
["FACEBOOK","160.58"]
]}
I can get a basic mapping working with:
require "json"
message = "{\"product\":\"midprice\",\"prices\":[[\"APPLE\",\"217.88\"],[\"GOOGLE\",\"1156.05\"],[\"FACEBOOK\",\"160.58\"]]}"
class Midprice
JSON.mapping(
product: String,
prices: Array(Array(String)),
)
end
midprice = Midprice.from_json(message)
p midprice.product # Outputs the String
p midprice.prices # Outputs
Crystal 0.26.1 Code: https://play.crystal-lang.org/#/r/515o
But ideally I would like prices to be a hash with the stock name as the key and the price as the value. Can this be done with JSON.mapping?