Convert charlist to string recursively

Hello. I need help from you :sob: :sob:
First of all, I have a return value from the neo4j database like this:

%{
  'records' => [
    %{
      'category' => 'NODE',
      'id' => 83,
      'key' => 'm',
      'labels' => ['Match'],
      'properties' => %{'bracket_id' => 1, 'depth' => 1}
    }
  ],
  'results' => %{
    'm' => %{
      'category' => 'NODE',
      'id' => 83,
      'key' => 'm',
      'labels' => ['Match'],
      'properties' => %{'bracket_id' => 1, 'depth' => 1}
    }
  }
}

The type of keys and values are charlist, not string(sometimes the value is integer though). I’d like to convert those charlist keys and values into string all!
Then, the data I want is like this:

%{
  "records" => [
    %{
      "category" => "NODE",
      "id" => 83,
      "key" => "m",
      "labels" => ["Match"],
      "properties" => %{"bracket_id" => 1, "depth" => 1}
    }
  ],
  "results" => %{
    "m" => %{
      "category" => "NODE",
      "id" => 83,
      "key" => "m",
      "labels" => ["Match"],
      "properties" => %{"bracket_id" => 1, "depth" => 1}
    }
  }
}

I think I have to make a function that converts charlist into string recursively, but I don’t have an implementation idea. Because I’d like to convert only charlist, not all list. Please help me if you have a solution for it. Thank you.

How you want to differentiate between [42] and '*'? Maybe look for another Neo4j driver that would use binaries instead of charlists?

Hi, thank you for replying. I get the data from python code, and I use erlport and it gives data in charlist.

def charlist_to_string(map) when is_map(map) do
    map
    |> Enum.map(fn {k, v} ->
      {charlist_to_string(k), charlist_to_string(v)}
    end)
    |> Map.new()
  end

  def charlist_to_string(list) when is_list(list) do
    if List.ascii_printable?(list) do
      List.to_string(list)
    else
      Enum.map(list, &charlist_to_string(&1))
    end
  end

  def charlist_to_string(val), do: val

I found ascii_printable?/2 and tried to write a function. But as @hauleth said, it cannot differentiate between [42] and *…

But documentation states that this should be translated to the binary

for python3 erlport does:

str -> list
bytes -> binary

wonder why they chose that…