sribe

sribe

Given the output of IO.inspect on a map, is there any way to parse that string back into a map?

Given the output of IO.inspect on a map, is there any way to parse that string back into a map?

For a one-shot hack, one could put them into source files and compile. For ongoing parsing out of a log, one could JSON encode them instead. But assume there’s a pile of maps written out by IO.inspect: is there a way to rehydrate them directly?

A tiny bit of context: I actually thought I had JSON in the files, but lolz, I do not.

Most Liked

al2o3cr

al2o3cr

One gotcha: inspect will truncate output by default if the maps are large. For instance:

0..100 |> Enum.with_index() |> Map.new() |> inspect() |> Code.eval_string()

fails (on my machine anyways) with:

warning: variable "..." does not exist and is being expanded to "...()", please use parentheses to remove the ambiguity or change the variable name
  nofile:1

** (CompileError) nofile:1: undefined function .../0 (there is no such import)
    (elixir 1.13.4) lib/code.ex:404: Code.validated_eval_string/3
lud

lud

For a single map you can do this:

 %{a: 1} |> inspect |> Code.eval_string() |> elem(0)

Now if you have more, you will have to find a way to split the string into valid chunks of code.

Now if this for a test or a quick made tool it’s fine but otherwise consider that eval is evil and be sure that there is no proper way of doing what you want before eval’ing code.

sribe

sribe

Thank you, eval_string is what I was missing.

I do have more, and I am already splitting them–I got to the part where I tried to parse them after splitting, Jason.decode kept failing, I looked closer and realized D’OH! NOT JSON!! :wink:

Where Next?

Popular in Questions Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43757 214
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54120 245
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement