dmstocking

dmstocking

To and from camelCase json api keys to snake_case elixir convention

I have an existing camelCase JSON API that I can’t change and I want to be able to continue to use elixir snake_case naming conventions. I am getting the JSON via UDP connection, and I send JSON responses back to that UDP endpoint. I am currently using Jason directly. It was included in the template, so I went with it. I figured out that I could provide my own decode function for keys so that I can change the JSON key to snake_case and run String.to_existing_atom/1. However this doesn’t appear to be an option in the encoding. I did find Convert camel case to underscore in json but it mostly talks about how to convert from camelCase to snake_case. It doesn’t talk about integrating into the JSON encoding. I could just recursively rebuild the entire struct into a map changing the keys, but that sounds like it would kill performance. Is there something I am missing? Should I just recursively convert each JSON payload I am sending? Should I switch to Poison? Should I not care about snake_case convention in Elixir?

Most Liked

axelson

axelson

Scenic Core Team

You might enjoy checking out recase:

Here’s a quick script to show how you could use it

Mix.install([:recase, :jason])

Recase.Enumerable.convert_keys(
  %{"yourKey" => "value"},
  &Recase.to_snake/1
)
|> IO.inspect(label: "res")

json =
Recase.Enumerable.convert_keys(
  %{my_key: "value"},
  &Recase.to_camel/1
)
|> IO.inspect(label: "to camel")
|> Jason.encode!()

IO.puts("Json: #{json}")

Running this prints out:

res: %{"your_key" => "value"}
to camel: %{myKey: "value"}
Json: {"myKey":"value"}
kevinschweikert

kevinschweikert

@mhanberg just released a new blog post and mentions his library schematic. Maybe that could help.
Here’s the Blog Post: Credo Language Server and the birth of elixir-tools | Mitchell Hanberg

Where Next?

Popular in Questions Top

LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
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
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
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
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement