I have a response body in json where I want to extract the value: ID_ADFA0741-6A46-4A90-8AAE-BC7DB18B27E1
In the below json structure.
.check(jsonPath("$..key").saveAs("key")))
gives me the value M01A
How could I use a check to obtain the value "key" for the element number 2 (the second occuerence of "key" below?
{"libItems":[{"hasNotes":false,"drugs":[{"ingredientNamesAndStrengths":[{"ingredientNames":["Diklofenak"],"strengths":["50 mg"]}],"order":0,"form":"Enterotab"}],"treatmentType":"Brand","isSupplement":false,"nameOfBrand":"Voltaren","nameFormStrength":"Diklofenak Enterotab 50 mg","medicationUsedFor":{"value":"TEST"},"clinicalWarnings":[],"dataWarnings":[{"practitionerName":"ELENA FOS ENGELUND","importDate":"2021-08-16T18:13:06.597+02:00","isNewTreatment":true,"type":"Import","isMarkedAsHandled":false}],"warningReferences":[{"key":"M01A","drugName":"Diklofenak Enterotab 50 mg","refType":"Atc"},{"key":"ID_ADFA0741-6A46-4A90-8AAE-BC7DB18B27E1","drugName":"Diklofenak Enterotab 50 mg","refType":"Atc"},{"key":"ID_BD1BDC3F-1FCF-4944-99BC-6B3E8EAAD6FD","drugName":"Diklofenak Enterotab 50 mg","refType":"Atc"}],"usage":"Fixed","atc":"M01AB05","atcNames":"Diklofenak","shortDose":{"key":"160","value":"1x3"},"drugId":"ID_BD1BDC3F-1FCF-4944-99BC-6B3E8EAAD6FD","hasIngredientNames":true,"prescriptionActions":["Stop","ConfirmUse","Renew","Prescribe"],"id":"440a01f5-1df8-4f26-a6c1-08d95cab00c7","treatmentId":"00be4297-cc9f-432d-30d6-08d95cab00c7","treatmentStart":"2021-06-02T00:00:00","dssnText":"1 tablett 3 ganger daglig","actionStatus":"ActionRequired","treatmentStatus":"Active","externalUpdate":"New","approvalStatus":"Empty","resepts":[{"localReseptState":"None","rfReseptState":"AvailableForDispatch"}],"diffPreviousValues":[],"hasRfError":false,"isLocked":false,"guardianAccessReservation":false,"paperReseptDispensation":false,"reseptPidState":"NoChange"}],"fibItems":[],"nibItems":[],"vaccines":[],"vibInformation":{"vibStatus":"ActionRequired","messagesToSign":{"new":0,"renewed":0,"changed":0,"stopped":0,"removed":0,"recalled":0,"registration":0,"stoppedLocalRegistration":0},"actionRequired":{"treatments":1,"nutritions":0,"consumables":0,"vaccines":0,"allergies":0,"warnings":0},"previousLibItems":["ID_BD1BDC3F-1FCF-4944-99BC-6B3E8EAAD6FD"]}}
Since, in the comments, the OP requested an example of using a Json parsing/serialization library for constructing case classes from the provided Json, for the accessing and processing of the desired fields; here is a simplified working example with circe
:
import io.circe._, io.circe.generic.auto._, io.circe.syntax._, io.circe.parser._
val myShortJsonString =
"""{"libItems":
[
{
"hasNotes":false,
"warningReferences":[
{"key":"M01A", "drugName":"Diklofenak Enterotab 50 mg", "refType":"Atc" },
{ "key":"ID_ADFA0741-6A46-4A90-8AAE-BC7DB18B27E1", "drugName":"Diklofenak Enterotab 50 mg", "refType":"Atc" },
{ "key": "ID_BD1BDC3F-1FCF-4944-99BC-6B3E8EAAD6FD", "drugName":"Diklofenak Enterotab 50 mg", "refType":"Atc" }
]
}
]
}"""
val myFullJsonString =
"""{"libItems":
[
{
"hasNotes":false,
"drugs":[
{"ingredientNamesAndStrengths":[{"ingredientNames":["Diklofenak"],"strengths":["50 mg"]}],
"order":0,
"form":"Enterotab"}
],
"treatmentType":"Brand",
"isSupplement":false,
"nameOfBrand":"Voltaren",
"nameFormStrength":"Diklofenak Enterotab 50 mg",
"medicationUsedFor":{"value":"TEST"},
"clinicalWarnings":[],
"dataWarnings":[{"practitionerName":"ELENA FOS ENGELUND","importDate":"2021-08-16T18:13:06.597+02:00","isNewTreatment":true,"type":"Import","isMarkedAsHandled":false}],
"warningReferences":[
{"key":"M01A","drugName":"Diklofenak Enterotab 50 mg","refType":"Atc"},
{"key":"ID_ADFA0741-6A46-4A90-8AAE-BC7DB18B27E1","drugName":"Diklofenak Enterotab 50 mg","refType":"Atc"},
{"key":"ID_BD1BDC3F-1FCF-4944-99BC-6B3E8EAAD6FD","drugName":"Diklofenak Enterotab 50 mg","refType":"Atc"}],
"usage":"Fixed",
"atc":"M01AB05",
"atcNames":"Diklofenak",
"shortDose":{"key":"160","value":"1x3"},
"drugId":"ID_BD1BDC3F-1FCF-4944-99BC-6B3E8EAAD6FD",
"hasIngredientNames":true,
"prescriptionActions":["Stop","ConfirmUse","Renew","Prescribe"],
"id":"440a01f5-1df8-4f26-a6c1-08d95cab00c7",
"treatmentId":"00be4297-cc9f-432d-30d6-08d95cab00c7",
"treatmentStart":"2021-06-02T00:00:00",
"dssnText":"1 tablett 3 ganger daglig",
"actionStatus":"ActionRequired",
"treatmentStatus":"Active",
"externalUpdate":"New",
"approvalStatus":"Empty",
"resepts":[{"localReseptState":"None","rfReseptState":"AvailableForDispatch"}],
"diffPreviousValues":[],
"hasRfError":false,
"isLocked":false,
"guardianAccessReservation":false,
"paperReseptDispensation":false,
"reseptPidState":"NoChange"
}
],
"fibItems":[],
"nibItems":[],
"vaccines":[],
"vibInformation":{
"vibStatus":"ActionRequired",
"messagesToSign":{
"new":0,
"renewed":0,
"changed":0,
"stopped":0,
"removed":0,
"recalled":0,
"registration":0,
"stoppedLocalRegistration":0
},
"actionRequired":{
"treatments":1,
"nutritions":0,
"consumables":0,
"vaccines":0,
"allergies":0,
"warnings":0
},
"previousLibItems":["ID_BD1BDC3F-1FCF-4944-99BC-6B3E8EAAD6FD"]
}
}"""
case class Library(libItems: List[LibItem],
// fibItems: List[FibItem], nibItems: List[NibItem], vaccines: List[Vaccine], vibInformation: VibInformation
)
case class LibItem(hasNotes: Boolean,
//drugs: List[Drug],
warningReferences: List[WarningReference])
case class WarningReference(key: String, drugName: String, refType: String)
val maybeLibraryJsonObject = parse(myShortJsonString)
val maybeLibrary = maybeLibraryJsonObject match {
case Right(libraryJsonObject) => libraryJsonObject.as[Library]
case Left(failure) => println(failure)
}
maybeLibrary match {
case Right(libraryResult) =>
println(libraryResult)
val libItems = libraryResult.asInstanceOf[Library].libItems
val libItem = libItems(0)
val warningReferenceKey = libItem.warningReferences(1).key
val warningReferenceKeySections = warningReferenceKey.split("-")
val code = warningReferenceKeySections(3)+"-"+warningReferenceKeySections(4)
println("code: " + code)
case Left(failure) => println(failure)
}
To make it work for the full json string, you need to create case classes for all of its fields, as in the commented section.