santoshbt

santoshbt

Output JSON response

Hi All,

Currently I have the list of companies in this format

companies= [
{“Please type employer name”, “”},
{“Google”, 2},
{“Accenture”, 3},
{“Microsoft”, 1},
{“Apple”, 4}
]

I want to output this in a JSON response.
I am trying with render function. But it is considering only the first tuple and throwing error.

(exit) an exception was raised:
** (Protocol.UndefinedError) protocol Jason.Encoder not implemented for {“Please type employer name”, “”}, of type Tuple, Jason.Encoder protocol must always be explicitly implemented. This protocol is implemented for the following type(s): MyFutureNow.TransferableBenefits, MyFutureNow.UntransferableBenefits, Ecto.Association.NotLoaded, Ecto.Schema.Metadata, Any, Atom, BitString, Date, DateTime, Decimal, Float, Integer, Jason.Fragment, List, Map, NaiveDateTime, Time

When I give normal string or simple map, it works fine. But how to give a list of tuples to render function to output as JSON response.

Please help.

TIA..

Marked As Solved

fuelen

fuelen

Hello,

JSON format has fewer types than Elixir language. It is not clear how to encode tuples and how to get tuples from JSON, so this task was put on developer’s shoulders.
You have to convert tuple to other JSON-compatible data type. The easiest way of doing this is to convert tuple to list right before encoding to JSON:

companies |> Enum.map(&Tuple.to_list/1) |> Jason.encode!()

Another way is to define an implementation of Jason.Encoder protocol for Tuple globally (read more here), but personally I don’t recommend this.

Where Next?

Popular in Discussions Top

andre1sk
A big advantage to Elixir is all the distributed goodness but for many applications running on multiple nodes having integrated Etcd, Zoo...
New
Rustixir
Hi everyone, im working on find best language/framework/system for high concurrency, high performance and stable performance after wor...
New
jeramyRR
This is an interesting article to read. Elixir’s performance, like usual, is excellent. However, it seems like the high CPU usage is co...
New
WolfDan
After doing a port from a c++ library to my project in phoenix I’ve seen that I need a faster way to run this algorithm and I found this ...
New
fireproofsocks
This is more of a general question, but I’m wondering how other people in the community think about the pattern matching in function sign...
New
praveenperera
How We Replaced React with Phoenix By: Thought Bot
New
IVR
Hi all, I’ve seen a number of related threads in the past, but I’d still be very curious to hear an up-to-date opinion on this topic. I...
New
acrolink
How does the two languages compare when it comes to server side application development? Any experiences or ideas? Thank you.
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
Markusxmr
Since Drab has been developed for a while in the open, introducing the Liveview functionality in a way it happend appears to undermine th...
New

Other popular topics Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
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
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
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
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New

We're in Beta

About us Mission Statement