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?

First Post!

NobbZ

NobbZ

Which keywordlist? I only see a list of maps. And how should the struct look like?

What is the JSON you need?

Can you post an example JSON how it should look like?

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

lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
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
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
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

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
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
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
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement