mxst

mxst

Use Poison to decode map of structs

Hey everyone,
I was wondering if there is a chance to decode am map from json to a map of predefined structs. Important is that the keys are not predefined.

So in the end I would love to transform this

{
  "text": "A pet food distribution center where the animal can tap the screen and receive a treat or meal. Would allow for healthy regulation and training.",
  "annotation_candidates": {
    "tap": {
      "text": "tap",
      "resource_candidates": [
        {
          "label": "Tap water",
          "resource": "http://dbpedia.org/resource/Tap_water"
        },
        {
          "label": "Tap dance",
          "resource": "http://dbpedia.org/resource/Tap_dance"
        }
      ]
    },
    "receive": {
      "name": "receive",
      "offset": 71,
      "resource_candidates": [
        {
          "label": "Radio receiver",
          "resource": "http://dbpedia.org/resource/Radio_receiver"
        }
      ]
    }
  ]
}

into this

%AnnotationCandidates{
  text: "A pet food distribution center where the animal can tap the screen and receive a treat or meal. Would allow for healthy regulation and training.",
  annotation_candidates: %{
    "tap" => %AnnotationCandidate{
      text: "tap",
      offset: 52,
      resource_candidates: [
        %ResourceCandidate{
          label: "Tap Water"
          resource: "http://dbpedia.org/resource/Tap_water",
                confidence: 0.2600142258675502
        }, 
        %ResourceCandidate{ ... }
      ]
    },
    "receive" => %AnnotationCandidate{ ... }
  }
}

I don’t know if that is possible (or a dumb idea)

Thank you for your help.

Most Liked

Fl4m3Ph03n1x

Fl4m3Ph03n1x

You can’t go from a JSON into a struct directly, but you can go from a JSON to a Map and then use the map to create a Struct.

I recommend you are a look at Jason. Once you have a map or a keyword list, have a look that struct(map, opts) and you will be ready to go.

axelson

axelson

Scenic Core Team

Structs have fixed keys. So if you don’t know the keys ahead of time then you can’t use a structural and need to use a map instead.

Where Next?

Popular in Questions Top

JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
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 record...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
Jim
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
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
vonH
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
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
hpopp
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
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

Other popular topics Top

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
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
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
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
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 record...
New
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
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

We're in Beta

About us Mission Statement