I have parsed the following JSON using Posion.decode!
json = %{"color-Black|size:10" =>
%{"attributes" => %{"color" => "Black","size" => "11"},
"isAvailable" => true,
"pricing" => %{"standard" => "$415.00", "sale" => 415}},
"color|size-EU:9.5" =>
%{"attributes" => %{"color" => "Black","size" => "11"},
"isAvailable" => true,
"pricing" => %{"standard" => "$415.00", "sale" => 415}}}
I want to map this and I'm unable to get JSON elements as the text in the node element changes. So far I've tried.
Enum.map(json , fn(item) ->
%{
color: item.attributes["color"],
size: item.attributes["size"],
price: item.pricing["standard"] * 100,
isAvailable: item.isAvailable
}
end)
But this code gives some error related to accessing.
While mapping the map, the iterated key-value pairs come to the mapper as tuples {key, value}
:
Enum.map(json, fn {_, %{"attributes" => attributes,
"isAvailable" => isAvailable,
"pricing" => pricing}} ->
%{
color: attributes["color"],
size: attributes["size"],
price: pricing["standard"],
isAvailable: isAvailable
}
end)
#⇒ [
# %{color: "Black", isAvailable: true, price: "$415.00", size: "11"},
# %{color: "Black", isAvailable: true, price: "$415.00", size: "11"}
# ]
Here we use an inplace pattern matching for values in mapper to simplify the code of matcher itself and make it less error-prone in a case of bad input.
Three things:
You have a map so Enum.map
will give you a tuple of key and value. You just want the value here, so do:
fn {_, item} ->
The keys in your map are strings. The dot syntax only works for atom keys. You need to do:
item["attributes"]
instead of
item.attributes
and similar for other keys.
The price you have is a string. You'll need to convert it to a Float before you can multiply it. You can do it like this using String.trim_leading
and String.to_float
:
iex(1)> "$123.45" |> String.trim_leading("$") |> String.to_float
123.45
Enum.map(json, fn {_, item} ->
%{
color: item["attributes"]["color"],
size: item["attributes"]["size"],
price: item["pricing"]["standard"] |> String.trim_leading("$") |> String.to_float |> Kernel.*(100),
isAvailable: item["isAvailable"]
}
end)
|> IO.inspect
Output:
[%{color: "Black", isAvailable: true, price: 4.15e4, size: "11"},
%{color: "Black", isAvailable: true, price: 4.15e4, size: "11"}]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With