1
votes

I'm trying to load a very large JSON file and bind it to a variable, here is my code that is failing.....

deps = File.open("../DepMap.json")
    |> IO.read(:all) 
    |> JSX.decode

I'm getting back

** (FunctionClauseError) no function clause matching in :io.request/2
(stdlib) io.erl:556: :io.request({:error, :enoent}, {:get_line, :unicode, ""})
(elixir) lib/io.ex:82: IO.do_read_all/2
lib/depchecker.ex:6: (module)
(stdlib) erl_eval.erl:670: :erl_eval.do_apply/6

What am I missing here? I'm assuming that the result of File.open gets passed as the first arguement to IO.read(:all) but this is the point of failure here and I'm not sure how to rectify this.

1

1 Answers

4
votes

File.open returns either {:ok, pid} or {:error, reason}. The second one in cases when it fails. Here you're getting {:error, :enoent} meaning the file does not exist - you'll probably need to figure out what's wrong with your path there.

You might also want to use the bang version of File.open in your pipeline:

deps = File.open!("../DepMap.json")
|> IO.read(:all) 
|> JSX.decode

This one behaves like File.open, but raises an exception instead of returning an error value. On success though it just returns the pid representing the file, which is what you need for the IO.read call.