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:

Nicd

Nicd

Another gotcha is that there’s no requirement for the inspect output to be valid Elixir. Many structs are inspected as something like #Foo<something> instead of %Foo{something}. It may not be feasible to parse your inspect output at all.

Obligatory note, evaling strings is very dangerous and a wide open invitation for remote code execution, if an attacker can control the inputs.

msimonborg

msimonborg

You can also Code.eval_file if it was written to a file.

File.write!("maps.txt", inspect([%{foo: :bar}, %{biz: :baz}]))
# => :ok
"maps.txt"
|> Code.eval_file()
|> elem(0)
# => [%{foo: :bar}, %{biz: :baz}]

Again, eval is very dangerous! Only use it if you trust the content 100%. Sounds like maybe you wrote the files though but just be careful.

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New

We're in Beta

About us Mission Statement