Is there a proper way of changing all the values of the fields with a given key using Scala Circe?
The scenario
I have n fields named validTo as dates (e.g. "2024-09-12T00:00:00.000+00:00") and I need to shift those dates by a given number of days, so I need a way to find and replace all those validTo fields in that JSON.
IMPORTANT NOTE: I don't know the JsonPath of those fields in advance, but only their key (name).
I see that there is a function, inspired by Play framework, to find all the values of a given key in Circe: io.circe.Json#findAllByKey:
/**
* Recursively return all values matching the specified `key`.
*
* The Play docs, from which this method was inspired, reads:
* "Lookup for fieldName in the current object and all descendants."
*/
final def findAllByKey(key: String): List[Json]
I will use something like this:
where in the
parseDatefunction you can handle as you want the date case, maybe even throwing exceptionAlternatively, you can follow what @Daenyth said and use a
Folder[json]: