din_S
Parse Request with nested JSON in phoenix
I have the following Json request
{
"id": "4",
"imp": [{
"id": "1",
"banner": {
"w": 300,
"h": 250,
"mimes": [
"image/jpg",
"image/gif",
"text/html",
"image/png",
"image/jpeg",
"application/javascript"
],
"hmin": 250
},
"bidfloor": 0.01,
"bidfloorcur": "USD",
},
{
"id": "2",
"banner": {
"w": 300,
"h": 250,
"mimes": [
"image/jpg",
"image/gif",
"text/html",
"image/png",
"image/jpeg",
"application/javascript"
],
"hmin": 250
},
"bidfloor": 0.033,
"bidfloorcur": "USD",
},],
"tmax": 2000,
}
I want to receive this json request and update some of its field and return it
I tried the following code
def index(conn, params) do
if (!params) do
Plug.Conn.send_resp(204)
else
IO.puts "RRR"
params=put_in(params["tmax"], params["tmax"]- 20)
params=put_in(Enum.at(params["imp"],0)["bidfloor"], Enum.at(params["imp"],0)["bidfloor"]+2)
Enum.each params["imp"], fn x ->
IO.inspect x
params=put_in(x["bidfloor"], x["bidfloor"]+20)
end
json(conn, params)
end
but this line
params=put_in(Enum.at(params["imp"],0)["bidfloor"], Enum.at(params["imp"],0)["bidfloor"]+2)
is only returning part of the request (the “imp” part)
what should I do?
Marked As Solved
andreaseriksson
Something like this
# map from json
map = %{
"id" => "4",
"imp" => [
%{
"banner" => %{
"h" => 250,
"hmin" => 250,
"mimes" => ["image/jpg", "image/gif", "text/html", "image/png",
"image/jpeg", "application/javascript"],
"w" => 300
},
"bidfloor" => 0.01,
"bidfloorcur" => "USD",
"id" => "1"
},
%{
"banner" => %{
"h" => 250,
"hmin" => 250,
"mimes" => ["image/jpg", "image/gif", "text/html", "image/png",
"image/jpeg", "application/javascript"],
"w" => 300
},
"bidfloor" => 0.033,
"bidfloorcur" => "USD",
"id" => "2"
}
],
"tmax" => 2000
}
# create a new imp
imp =
Map.get(map, "imp")
|> Enum.map(fn %{"bidfloor" => bidfloor} = node ->
Map.put(node, "bidfloor" (bidfloor + 2))
end)
# Add imp to the initial map
Map.put(map, "imp", imp)
2
Also Liked
andreaseriksson
I think it should be Enum.map(bid, fn %{"bidfloor" => bidfloor} = node2 ->
1
Popular in Questions
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
can someone please explain to me how Enum.reduce works with maps
New
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
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
Student & New to elixir. Nice language.
I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
I tried installing
elixir 1.11.2
erlang 23.3.4
via asdf in my zsh shell. Enabled the versions locally and globally.
When I list them ...
New
Hello, I have map which I want to convert it to string like this:
the map:
%{last_name: "tavakkoli", name: "shahryar"}
the string I ne...
New
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
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
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir
iex(2)...
New
Other popular topics
Hello!
tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability.
After spen...
New
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
Hi,
I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
New
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
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
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New








