A table has a field in it which structure can vary from record to record, that’s why I’ve made it a JSON type. Think of “payment_methods” table where “payment_details” is such a json field: for some payment method it’s enough to provide some wallet_id, for others, such as banks, more details are needed. But there’s no common structure. Hence, json.
Now I want to be able to work with said json field on an html page - create a record with it and edit it too.
A part of a migration:
add(:my_field, :map)
A part of an Elixir model:
field(:my_field, :map)
On an html page I have textarea in which I’ll enter a string such as:
Doesn’t the data from the form come as a string? I don’t think Phoenix automagically parses it into an Elixir map. So params["my_map_data"] must contain a string and that’s why the changeset error mentions that the type is invalid.
That’s what Jason.decode! is used for, to decode a JSON text into an Elixir value.
You should probably use the Jason.decode function without the ! and check whether you get any errors.