{
...
"otherAttr" : 123,
"city1" : "",
"city2" : "Cupertino",
"yetAnotherAttr" : 123,
...
}
type alias User =
{...
, otherAttr : Int,
, city : String
, yetAnotherAttr : Int,
...
}
userDecoder : Decoder User
userDecoder =
decode user
|> ...
|> required "otherAttr" int
|> cityDecoder
|> required "yetAnotherAttr" int
|> ...
cityDecoder : Decoder String
cityDecoder =
Decode.at [ "city1", "city2" ] Decode.string -- Not working !!
userDecoder : Decoder User
userDecoder =
decode User
|> required "otherAttr" int
|> custom cityDecoder
|> required "yetAnotherAttr" int
cityDecoder : Decoder String
cityDecoder =
Decode.oneOf
[ Decode.field "city1" notEmptyStr
, Decode.field "city2" notEmptyStr
]
notEmptyStr : Decoder String
notEmptyStr =
Decode.string
|> Decode.andThen
(\str ->
if String.isEmpty str then
Decode.fail "string length 0"
else
Decode.succeed str
)
Decode.at [ "city1", "city2" ] ...
{
"city1": {
"city2": "value"
}
}
Hi,
I have the following json that i want to Decode :
{
Enter code here...