Created
March 30, 2022 16:24
-
-
Save ream88/078c53e59f27073f9a874429defd496c to your computer and use it in GitHub Desktop.
Convert Twilio JSON responses to XML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env elixir | |
Mix.install([ | |
:jason, | |
:xml_builder | |
]) | |
[input] = System.argv() | |
defmodule Helper do | |
def map_to_elements(map) do | |
map | |
|> Enum.reduce([], fn | |
{key, map}, list when is_map(map) -> | |
[XmlBuilder.element(format_key(key), [], map_to_elements(map)) | list] | |
{key, value}, list -> | |
[XmlBuilder.element(format_key(key), [], value) | list] | |
end) | |
|> Enum.reverse() | |
end | |
defp format_key(key) do | |
key |> Macro.camelize() |> String.to_atom() | |
end | |
end | |
elements = | |
input | |
|> File.read!() | |
|> Jason.decode!() | |
|> Helper.map_to_elements() | |
XmlBuilder.document(:TwilioResponse, [XmlBuilder.element(:Call, [], elements)]) | |
|> XmlBuilder.generate() | |
|> IO.puts() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment