How to bundle data (eg, JSON) in a hex dependency

I want to bundle JSON data with a hex package.

Consider this function:

def list_tlds() do!("data/tlds.json") |> Jason.decode!()

The file data/tlds.json exists, and of course it works locally (relative to the directory I run mix), but when this package is a hex dependency, data/tlds.json can’t be found.

I’ve had several use-cases for bundling JSON data with a hex package:

  • Domain registrar client library: bundling a list of TLDs supported by the registrar.
  • OEmbed library: bundle a list of common providers.

By bundling this data in its JSON source format, it makes it easier to update. If the source data changes I can simply download the new version and update my package.

Is this possible?

Sure can, and its quite a common pattern. The function Application.app_dir/1 or Application.app_dir/2 will help. Using your example:

content =
  |> Application.app_dir("data")
  |> Path.join("tlds.json")
  |> Jason.decode!()

Here :my_app is the name of your library app.


Thank you! That’s fantastic.