din_S

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

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)

Also Liked

andreaseriksson

andreaseriksson

I think it should be Enum.map(bid, fn %{"bidfloor" => bidfloor} = node2 ->

Where Next?

Popular in Questions 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
Tee
can someone please explain to me how Enum.reduce works with maps
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Kagamiiiii
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
fayddelight
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
shahryarjb
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
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
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
idi527
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 Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
William
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
rms.mrcs
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
belgoros
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
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
joeerl
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

We're in Beta

About us Mission Statement