Play JSON transformer for copying value from optional field

This is another JSON transformer problem where I need some help, next to this one

Given the following JSON where fileKey is an optional field (can be present or not):

{
	"invoiceState": {
		"userId": "832eac3d-89d2-428c-9007-688b3e590533",
		"invoices": {
			"407613d7-7dc0-4cd6-8d62-41eea7067ca0": {
				"fileKey": {
					"key": "407613d7-7dc0-4cd6-8d62-41eea7067ca4"
				},
				"externalId": "I-000000-000000"
			},
			"8d16b2f8-ced0-4b41-8b6b-b275709ba88a": {
				"externalId": "I-000000-000001"
			}
		}
	}
}

I would like to transform it to this:

{
	"invoiceState": {
		"userId": "832eac3d-89d2-428c-9007-688b3e590533",
		"invoices": {
			"407613d7-7dc0-4cd6-8d62-41eea7067ca0": {
				"file": {
					"path": "407613d7-7dc0-4cd6-8d62-41eea7067ca4",
					"createdTime": "Some UTC time"
				},
				"externalId": "I-000000-000000"
			},
			"8d16b2f8-ced0-4b41-8b6b-b275709ba88a": {
				"externalId": "I-000000-000001"
			}
		}
	}
}

The main problem is the fact that the fileKey field is optional.

I would also appreciate some help here.

After a long try and error loop I now found a solution which seems to work for my case:

def fileKeyToFileMetadata: Reads[JsValue] =
      JsPath.read[JsString].map {
        case JsString(s) =>
          Json
            .toJson(
              FileMetadata(Paths.get(s), Instant.EPOCH)
            )
            .as[JsObject]
      }

    val invoices: Reads[JsObject] =
      (JsPath \ "file").json
        .copyFrom(
          (JsPath \ "fileKey" \ "key").json.pick(fileKeyToFileMetadata)
        )
        .orElse(JsPath.json.prune)

    (JsPath \ "invoiceState" \ "invoices").json
      .update(
        Reads
          .map(invoices)
          .map(JsObject(_))
      )