pedromvieira

pedromvieira

TransformMap - Deeply Nested Maps into flat maps or 2 dimensional array. Export any map to CSV, XLSX or JSON

Transform Elixir Deeply Nested Maps into flat maps or 2 dimensional array. Export any map to CSV, XLSX or JSON.

This will help in converting any deep nested map into a flap map or array to export it. Tested with huge maps and several thousands lines.

Please give your feedback.

https://hex.pm/packages/transform_map

https://github.com/pedromvieira/transform_map

Usage:

  iex> TransformMap.multiple_shrink(map, "|", true, true)

  iex> TransformMap.multiple_expand(shrink_map, "|", true)

  iex> TransformMap.multiple_to_array(map, "|", true, true)

  iex> TransformMap.multiple_keys(shrink_map, true)

  iex> TransformMap.multiple_to_array(map, "|", true, true) |> TransformMap.Export.to_csv("test.csv", true)

  iex> TransformMap.multiple_to_array(map, "|", true, true) |> TransformMap.Export.to_xlsx("test.xlsx", true)

  iex> map |> TransformMap.Export.to_json("test.json", true)

  iex> "temp/test.csv" |> TransformMap.Export.to_gzip()

Most Liked

pedromvieira

pedromvieira

It uses some libraries to do properly escape and coding. By default it’s all UTF-8 with basic escaping, but you can use your own functions.
Give it a try! :grinning:

External Libraries:

JSON - exjsx | Hex
CSV - csvlixir | Hex
XLSX - elixlsx | Hex

And for performance:

PARALLEL STREAM - parallel_stream | Hex

Example:

map = [
  %{
    "action" => "sent",
    "data" => %{
      "channel" => "email",
      "message" => %{
        "campaign" => 1,
        "editor_id" => 1,
        "group" => 2,
        "list_type" => "regular",
        "schedule" => 4,
        "shortener" => "default",
        "target_data" => %{
          "birthday" => "1982-06-09T15:00:00",
          "businessunit" => "Board",
          "locale" => "eng",
          "location_data" => %{
            "city" => "São Paulo",
            "city_data" => %{"mayor" => "John Snow", "population" => 30000000}
          },
          "nome" => "Pedro",
          "role" => "CTO"
        }
      },
      "provider" => "sendgrid"
    },
    "id" => 8,
    "inserted_at" => "2018-04-02 18:07:52.260159",
    "key" => "cGhpc2h4fDF8NHwyfHJlZ3"
  }
]

shrink_map = TransformMap.multiple_shrink(map, "|", true, true)
[
  %{
    "action" => "sent",
    "data|channel" => "email",
    "data|message|campaign" => 1,
    "data|message|editor_id" => 1,
    "data|message|group" => 2,
    "data|message|list_type" => "regular",
    "data|message|schedule" => 4,
    "data|message|shortener" => "default",
    "data|message|target_data|birthday" => "1982-06-09T15:00:00",
    "data|message|target_data|businessunit" => "Board",
    "data|message|target_data|locale" => "eng",
    "data|message|target_data|location_data|city" => "São Paulo",
    "data|message|target_data|location_data|city_data|mayor" => "John Snow",
    "data|message|target_data|location_data|city_data|population" => 30000000,
    "data|message|target_data|nome" => "Pedro",
    "data|message|target_data|role" => "CTO",
    "data|provider" => "sendgrid",
    "id" => 8,
    "inserted_at" => "2018-04-02 18:07:52.260159",
    "key" => "cGhpc2h4fDF8NHwyfHJlZ3"
  }
]

array = TransformMap.multiple_to_array(map, "|", true, true)
[
  ["action", "data|channel", "data|message|campaign", "data|message|editor_id",
   "data|message|group", "data|message|list_type", "data|message|schedule",
   "data|message|shortener", "data|message|target_data|birthday",
   "data|message|target_data|businessunit", "data|message|target_data|locale",
   "data|message|target_data|location_data|city",
   "data|message|target_data|location_data|city_data|mayor",
   "data|message|target_data|location_data|city_data|population",
   "data|message|target_data|nome", "data|message|target_data|role",
   "data|provider", "id", "inserted_at", "key"],
  ["sent", "email", 1, 1, 2, "regular", 4, "default", "1982-06-09T15:00:00",
   "Board", "eng", "São Paulo", "John Snow", 30000000, "Pedro", "CTO",
   "sendgrid", 8, "2018-04-02 18:07:52.260159", "cGhpc2h4fDF8NHwyfHJlZ3"]
]

array |> TransformMap.Export.to_csv("test.csv", true)
{:ok, ".../temp/test.csv.gz"}

CSV
action,data|channel,data|message|campaign,data|message|editor_id,data|message|group,data|message|list_type,data|message|schedule,data|message|shortener,data|message|target_data|birthday,data|message|target_data|businessunit,data|message|target_data|locale,data|message|target_data|location_data|city,data|message|target_data|location_data|city_data|mayor,data|message|target_data|location_data|city_data|population,data|message|target_data|nome,data|message|target_data|role,data|provider,id,inserted_at,key
sent,email,1,1,2,regular,4,default,1982-06-09T15:00:00,Board,eng,São Paulo,John Snow,30000000,Pedro,CTO,sendgrid,8,2018-04-02 18:07:52.260159,cGhpc2h4fDF8NHwyfHJlZ3

OvermindDL1

OvermindDL1

Can you give some examples of, say, at least 4-deep nested maps being converted to a csv as well as some other examples? Also it does proper csv escaping and xlsx and so forth? Is there a way to decorate the xlsx nodes with formatting data for better displaying to the users?

Where Next?

Popular in Libraries Top

New
Qqwy
While not as prevalent as in imperative languages, arrays (collections with efficient random element access) are still very useful in Eli...
New
vic
Expat is a tiny experiment I did for extracting patterns and being able to reuse them (compose and share patterns between elixir librarie...
New
mplatts
With HEEX released we decided to start a components library using Tailwind CSS - check it out here: Petal Components. We also have a boi...
New
tmbb
I’ve been working on two packages (not on hex.pm yet) to build admin interfaces for phoenix apps: bureaucrat - which contains a bunch ...
New
praveenperera
FastRSS Parse RSS feeds very quickly: This is rust NIF built using rustler Uses the RSS rust crate to do the actual RSS parsing Speed...
New
zoltanszogyenyi
Hey everyone :wave: Excited to join this forum - I am one of the founders and current project maintainers of a popular and open-source U...
New
engineeringdept
I’ve just released the first version of Snap, an Elasticsearch client. It borrows ideas about application structure and process managemen...
New
Crowdhailer
Experimenting with this code. OK.try do user <- fetch_user(1) cart <- fetch_cart(1) order = checkout(cart, user) save_or...
New
anshuman23
Hello all, I have been working on my proposed project called Tensorflex as part of Google Summer of Code 2018.. Tensorflex can be used f...
New

Other popular topics Top

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
yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
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
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
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 42633 214
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
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
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

Sub Categories:

We're in Beta

About us Mission Statement