I have a JSON string with array as the following:
{
"cars": {
"Nissan": [
{"model":"Sentra", "doors":4},
{"model":"Maxima", "doors":4}
],
"Ford": [
{"model":"Taurus", "doors":4},
{"model":"Escort", "doors":2}
]
}
}
I would like to edit a new card brand, using circe at scala.
Instead of
"Nissan": [
{"model":"Sentra", "doors":4},
{"model":"Maxima", "doors":4},
]
I would like to have as a result:
"Nissan": [
{"model":"Sentra", "doors":1000},
],
Thanks.
Made after reading the manual, please do the same next time!
Apart from your JSON being invalid (Trailing comma on object in array, it should look something like this:"Nissan"
)Try it out! (Rerun to see correct indentation!)
newJson
is actually anOption
, so if parsing or modifying fails, you will getNone
.Calling
toOption
onparse(json)
converts the returnedEither[Json]
(parsing succeeded / failed) to anOption[Json]
.We need to use an
Option[Json]
in the first place because.top
returns aOption[Json]
(modifying succeeded / failed), too.This way we can
flatMap
and don't have to deal with any nested types (see here).