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.

beatscode

beatscode

I was looking at using guards with functions but doesn’t seem like I can use Map.get inside the guard.
I would like to create related functions as well. Can I not make related functions with structs?

NobbZ

NobbZ

You can’t access maps (structs are just sugar over maps) in a guard, but you can match on them in the function head or in a case-clause or using the match operator.

I do not understand that question.

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
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
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
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
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
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
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
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
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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
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
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement