Skip to content

Instantly share code, notes, and snippets.

@ream88
Created March 30, 2022 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ream88/078c53e59f27073f9a874429defd496c to your computer and use it in GitHub Desktop.
Save ream88/078c53e59f27073f9a874429defd496c to your computer and use it in GitHub Desktop.
Convert Twilio JSON responses to XML
#!/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