Convert all value to string

if have a variable like :

result =
      from(Schema, where: ^whitelisted_params) |> limit(^query_limit) |> Repo.all(),
      fn elem ->
         elem |> Map.from_struct() |> Map.delete(:__meta__)

there is int and string value in Schema and i’m trying to convert all values from it to become string


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()
  |>, 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 =
    |> Repo.all
    |> x ->
        |> Map.delete(:__meta__)
        |> {k, v} -> {k, to_string(v)} end)

This would be all vanilla, but if you are in a phoenix project you could use Poison with Poison.encode

Just in case, the inner |> {k, v} -> {k, to_string(v)} end) would return a list, not a map.


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