Sardoan

Sardoan

Change keyword list to map for json encoding

Hi,
I have this structure:

[%{"next" => "http://www.example.com/test?foo=bar&page=4"},
%{"prev" => "http://www.example.com/test?foo=bar&page=2"},
%{"first" => "http://www.example.com/test?foo=bar&page=1"},
%{"last" => "http://www.example.com/test?foo=bar&page=5"}]

With Poison.encode! I get following:

"[{\"next\":\"http://www.example.com/test?foo=bar&page=4\"},
{\"prev\":\"http://www.example.com/test?foo=bar&page=2\"},
{\"first\":\"http://www.example.com/test?foo=bar&page=1\"},
{\"last\":\"http://www.example.com/test?foo=bar&page=5\"}]"

After parsing in JS with JSON.parse I recieve an array with 4 fields. What I need is a simple struct without an array, so I can access it in JS via struct.next, sruct.prev

How can I transform the keyword list to a struct, which gives me the JSON I need?
Any idea?

Most Liked

NobbZ

NobbZ

You can transform the list, as shown by @LostKobrakai, but you really should consider using a map on the elixir side from the beginning.

A list of maps, one key each feels wrong, especially when you say, that you consider this a single entity at the other end of the application.

LostKobrakai

LostKobrakai

For your example Enum.reduce(list, &Map.merge/2) should work, but I’d first consider why you’re having that list in the first place.

peerreynders

peerreynders

This was really the core issue because had they been tuples:

iex(1)> key_values = [
...(1)>   {"next","http://www.example.com/test?foo=bar&page=4"},
...(1)>   {"prev","http://www.example.com/test?foo=bar&page=2"},
...(1)>   {"first","http://www.example.com/test?foo=bar&page=1"},
...(1)>   {"last","http://www.example.com/test?foo=bar&page=5"}
...(1)> ]
[
  {"next", "http://www.example.com/test?foo=bar&page=4"},
  {"prev", "http://www.example.com/test?foo=bar&page=2"},
  {"first", "http://www.example.com/test?foo=bar&page=1"},
  {"last", "http://www.example.com/test?foo=bar&page=5"}
]
iex(2)> my_map = Map.new(key_values)
%{
  "first" => "http://www.example.com/test?foo=bar&page=1",
  "last" => "http://www.example.com/test?foo=bar&page=5",
  "next" => "http://www.example.com/test?foo=bar&page=4",
  "prev" => "http://www.example.com/test?foo=bar&page=2"
}
iex(3)>

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
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
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
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
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

Other popular topics Top

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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
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
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
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
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
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
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
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement