beatscode

beatscode

Convert json to defstruct definition

I’m porting an application written in go to elixir. I’m just starting to learn elixir. In go it is easier to convert your data to structs to easily get properties from objects. I’d like to do the same in elixir. I’d like to convert a json payload to a set of defstructs. I see the docs on defstruct but I am wondering what developers do when the defstruct needed is too tedious to write. Are there any tools to convert a json payload to a defstruct definition?
I see the struct function but I would still need to write out the defstruct definition to make that function work. I am also using poison to convert the payload to a map but I’d like to use custom types instead.

Most Liked

NobbZ

NobbZ

I said, that I avoid creating defstruct only modules. If though I have related functions for some type, I do create a module and a defstruct as well. Thats it.

Also JSON doesn’t give you any guarantee about types, so why care?

Creating and maintaining depply nested structs is a burden and comes with a technical debt, when in fact all you need to rely on is the contract that there will be certain fields and you never care for the rest.

This is especially true when you have a JSON which constantly changes its shape and you need to find out the proper type first by checking a certain key in it.

OvermindDL1

OvermindDL1

If using Poison you can have it auto-convert a JSON string to a specific struct(s). You’d still need to write your structs, but once done then you have a confirmed structure.

NobbZ

NobbZ

Why do you need it to be a struct? Can’t you use a map instead?

I do usually try to avoid to create struct-only modules, so unless you do not want to create related functions, using a plain map might be so much easier.

Where Next?

Popular in Questions Top

New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
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
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47930 226
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New

We're in Beta

About us Mission Statement