Hi all, I have a couple of questions on the matter:
I have a JSON where some fields are generally numbers (ie 2020) but sometimes they are stored as strings which represent a number (ie “2020”).
How to silently decode those type of fields into an Elm Int?
Some JSON data comes with different structure. Sometimes it is a simple object which can be decoded as a Elm Record, other times it could be a list of different objects which should be decoded as List of Records. (in the two cases, even the single objects have different formats).
Ie:
{“prop1”: “”, “prop2”: }
or
[{“prop1”: “”, “prop2”: , “prop3”: “”}, …]
I have tried to model this situation with types, but I’m stuck on decoding.
something like the following:
type alias MetadataType1 =
{ prop1: String
, prop2: Int
}
type alias MetadataType2 =
{ prop1: String
, prop2: Int
, prop3: String
}
type Metadata
= SingleMetadata MetadataType1
| MultiMetadata (List MetadataType2)
-- the object which should be the result of decoding where metadata can be of different types
type alias JsonData =
{ name: String
, metadata: Metadata
}