acrolink
How to return a structured JSON with column names and values in Mariaex
I am using Mariaex without Ecto to query a MySQL database for one of my projects, I get results like this:
%Mariaex.Result{
columns: ["type", "voucher", "item_code", "sum"],
connection_id: #PID<0.1220.0>,
last_insert_id: nil,
num_rows: 14,
rows: [
[11, "000036", "0004", 40.0],
[11, "000180", "0011", 10.0],
[11, "000038", "0041", 5.0],
[11, "000180", "0042", 5.0],
[11, "000037", "0048", 65.0],
[11, "000036", "0074", 5.0],
[11, "000036", "0081", 10.0],
[11, "000036", "0083", 16.0],
[11, "000038", "0087", 5.0],
[11, "000038", "0089", 5.0],
[11, "000036", "0092", 10.0],
[11, "000035", "0093", 15.0],
[11, "000037", "0097", 37.0],
[11, "000243", "0100", 6.0]
]
}
How to return this data as JSON like this:
{
"type": 11,
"voucher": "000036",
..
},
{
"type": 11,
"voucher": "000180",
..
}
Thank you.
Marked As Solved
idi527
You can transform it yourself, probably. According to your examples, it would be something like
@spec to_maps(Mariaex.Result.t()) :: [map()]
def to_maps(%Mariaex.Result{columns: columns, rows: rows}) do
Enum.map(rows, fn row ->
columns
|> Enum.zip(row)
|> Enum.into(%{})
end)
end
and then you can use any of the json encoders
result # Mariaex.Result.t()
|> to_maps()
|> Jason.encode!()
2
Popular in Questions
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
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
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 record...
New
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
I would like to know what is the best IDE for elixir development?
New
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
I have a super simple question about elixir - how would I take a file like this
foo bar baz
and output a new file that enumerates th...
New
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
Other popular topics
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
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
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Hi folks,
Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
New
As a follow up to my earlier question:
I have the code compiling and running but not getting a successful login from the rest server. ...
New
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
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
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
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
I have a super simple question about elixir - how would I take a file like this
foo bar baz
and output a new file that enumerates th...
New







