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.

Last Post!

dimitarvp

dimitarvp

How do you want tuples encoded in your JSON?

Where Next?

Popular in Discussions Top

scouten
I’m looking for a host for the server part of a small (personal) side project that I’m working on. It’s currently written in Node.js and ...
New
ricklove
I was just introduced to Elixir and Phoenix. I was told about the 2 million websocket test that was done 2 years ago. From my research, t...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
RudManusachi
What configs will make sense to put to runtime.exs? – A bit of how I configure apps: I have generic configs in config/config.exs, dev...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
crispinb
On reading dhh’s latest The One Person Framework it strikes me that Phoenix with LiveView is already pretty much this. However, never hav...
New
arcanemachine
https://nitter.net/josevalim/status/1744395345872683471 https://twitter.com/josevalim/status/1744395345872683471
New

Other popular topics Top

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
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
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

We're in Beta

About us Mission Statement