I am new to elixir and I am trying to get some text from a very nested map.
So I am doing a get request to this link and I am decoding it with Jason.decode.
What I want to do is iterate over it and get every text value (sections->0->content->0->text).
In the end I just want it to be a big string of all the text values
(the link can always change so there might be more maps etc)
Thanks in advance!
You can use
Enumwith the pipe operator|>to enumerate the data structure and extract the parts you want.For example:
A breakdown:
Map.get("sections")selects the contents of thesections.Enum.reject(...)ignores empty sections.Enum.flat_mapiterates over the sections, gets thecontentsof each section, transforms it using the inner function, and then flatten the result into a single list.Enum.filter(...)only process contents whosetypeproperty istextorparagraph.Enum.mapextracts thetextproperty from each contents map.Enum.joinjoins the resulting list of strings with a newline character.