if have a variable like :
result =
Enum.map(
from(Schema, where: ^whitelisted_params) |> limit(^query_limit) |> Repo.all(),
fn elem ->
elem |> Map.from_struct() |> Map.delete(:__meta__)
end
)
there is int and string value in Schema
and i’m trying to convert all values from it to become string
idi527
February 2, 2019, 1:07pm
2
You can probably use Map.take
with Schema.__schema__(:fields)
, there is no need to “delete” :__meta__
.
fields = Schema.__schema__(:fields)
result =
from(Schema, where: ^whitelisted_params)
|> limit(^query_limit)
|> Repo.all()
|> Enum.map(&Map.take(&1, fields))
After that you can either make all values in the map to be strings or you can make the whole map to be a string, I’m not sure what exactly you are after.
Something like:
result =
query
|> Repo.all
|> Enum.map(fn x ->
Map.from_struct(x)
|> Map.delete(:__meta__)
|> Enum.map(fn {k, v} -> {k, to_string(v)} end)
end)
This would be all vanilla, but if you are in a phoenix project you could use Poison
with Poison.encode
idi527
February 2, 2019, 1:13pm
4
Just in case, the inner |> Enum.map(fn {k, v} -> {k, to_string(v)} end)
would return a list, not a map.
2 Likes
is that will convert all map value to string ?, i just try it and integer value still exists
“After that you can either make all values in the map to be strings” how ?
What do you mean a string? What encoding? Maps do not have a string representation. You can encode a map as something like JSON and have a JSON string.
1 Like
yes sir, i want all json return value to become string