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 Announcing Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
New
seancribbs
Today I released a new dialyzer Mix task as the dialyzex package! At the time we started writing this task, the existing dialyzer integra...
New
maltoe
Hello! Came here to announce ChromicPDF, a pet project PDF generator I’ve been working on for the past few months. Why another PDF gener...
New
zorbash
I created Kitto a framework for dashboards inspired by Dashing. The distributed characteristics of Elixir and the low memory footprint...
New
woutdp
Hi! I wanted to introduce my latest project LiveSvelte. It allows you to render Svelte inside LiveView with end-to-end reactivity. It’s ...
New
kip
ex_cldr provides localisation and internationalisation support based upon the data from the Unicode CLDR project. Unicode released CLDR ...
407 12840 120
New
blatyo
The best overview for how things are tied together is this presentation. Modules and functions are pretty well documented at this point, ...
New
cjen07
parameterized pipe in elixir: |n> edit: negative index in |n> and mixed usage with |> are supported example: use ParamPipe ...
New
archan937
It is a well-know topic within the Elixir community: “To mock or not to mock? :)” Every alchemist probably has his / her own opinion con...
New
wfgilman
I’ve cleaned up and open sourced three financial libraries I was using for my company. They are bindings for the APIs of these three comp...
New

Other popular topics Top

greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
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
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

We're in Beta

About us Mission Statement