Parsing Dynamic response in Karate - api

I want to verify the value of "RequestedName" in the following response, where the keys for different drugs is dynamic:
{
"requestId": "c826bee1-610e-4dee-b998-1fe4f8c15a1b",
"requestSource": "",
"responseSource": "client",
"status": 200,
"responseCodes": [
{
"code": "S00001",
"message": "Success.",
"params": {
"entity": ""
}
}
],
"context": null,
"payload": {
"0113ccf86ba79b698b8e7a8fb9effc4b": {
"RequestedName": "paracetamol",
"SearchKey": "0113ccf86ba79b698b8e7a8fb9effc4b",
"Name": "Genexa Acetaminophen Extra Strength",
"PrescribableName": "",
"ProductCodes": [
"69676-0059"
],
"DosageForm": "Tablet, coated",
"Route": "Oral",
"Approved": false,
"UnApproved": false,
"Generic": false,
"Allergen": false,
"Vaccine": false,
"Strength": {
"Number": "500",
"Unit": "mg/1"
},
"Purposes": {},
"SideEffects": {}
},
"0349fa4ea29da419c46745bc7e2a6c07": {
"RequestedName": "paracetamol",
"SearchKey": "0349fa4ea29da419c46745bc7e2a6c07",
"Name": "Pain Reliever",
"PrescribableName": "",
"ProductCodes": [
"70677-0168"
],
"DosageForm": "Tablet, extended release",
"Route": "Oral",
"Approved": true,
"UnApproved": false,
"Generic": true,
"Allergen": false,
"Vaccine": false,
"Strength": {
"Number": "650",
"Unit": "mg/1"
},
"Purposes": {},
"SideEffects": {}
},
"060cfbde5d82d947c56aac304c136fd3": {
"RequestedName": "paracetamol",
"SearchKey": "060cfbde5d82d947c56aac304c136fd3",
"Name": "Betr Pain Relief",
"PrescribableName": "Acetaminophen 500 mg Oral Tablet",
"ProductCodes": [
"80267-0484"
],
"DosageForm": "Tablet",
"Route": "Oral",
"Approved": false,
"UnApproved": false,
"Generic": false,
"Allergen": false,
"Vaccine": false,
"Strength": {
"Number": "500",
"Unit": "mg/1"
},
"Purposes": {},
"SideEffects": {}
},
"0950fcbac262c1c1d3a9e6630615a5f9": {
"RequestedName": "paracetamol",
"SearchKey": "0950fcbac262c1c1d3a9e6630615a5f9",
"Name": "Acetaminophen",
I tired this:
* def list = []
* def fun = function(k, v){ karate.appendTo('list', { key: k, val: v } )}
* karate.forEach(response, fun)
* def keys = $list[?(#.val.payload.RequestedName==drugName)].key
but not working, getting error as below:
def keys = $list[?(#.val.payload.RequestedName==drugName)].key
Failed to parse filter: [?(#.val.payload.RequestedName==drugName)], error on position: 32, char: d
testsuite/GetDrugs.feature:20

Here is the approach you can use:
* def response =
"""
{
dynamicKey1: {
fixedKey: 'fixedValue1',
dataKey: 'dataValue2'
},
dynamicKey2: {
fixedKey: 'fixedValue2',
dataKey: 'dataValue2'
}
}
"""
* def keys = []
* def fun = function(k, v){ if (v.fixedKey == 'fixedValue2') keys.push(k) }
* karate.forEach(response, fun)
* match keys == ['dynamicKey2']

Related

jsonb_set not working as expected with nested collections, resulting in null value returned

I am trying to update a nested collection in my Postgres table using json_set, however, my approach is resulting in a collection of null values.
id
payload
row_version
fbfd3b9d-bb20-4c1b-985f-0979890472ec
{ "slots": [ { "bannerXCreatorSettings": { "additionalFields": [ { "label": "test-label", "enabled": true, "mandatory": false, "additionalFieldId": "label", "maxCharacterLimit": 22, "additionalFieldType": "TEXT", "colorHexOptionsList": [] }, { "label": "test-label-2", "enabled": true, "mandatory": false, "additionalFieldId": "label2", "maxCharacterLimit": 55, "additionalFieldType": "TEXT", "colorHexOptionsList": [] } ] } } ]}
1
I have the following json payload stored in my table:
{
"slots": [
{
"bannerXCreatorSettings": {
"additionalFields": [
{
"label": "test-label",
"enabled": true,
"mandatory": false,
"additionalFieldId": "label",
"maxCharacterLimit": 22,
"additionalFieldType": "TEXT",
"colorHexOptionsList": []
},
{
"label": "test-label-2",
"enabled": true,
"mandatory": false,
"additionalFieldId": "label2",
"maxCharacterLimit": 55,
"additionalFieldType": "TEXT",
"colorHexOptionsList": []
}
]
}
}
]
}
Within the additionalFields collections, I want to add selectOptions. Like this:
{
"slots": [
{
"bannerXCreatorSettings": {
"additionalFields": [
{
"label": "test-label",
"enabled": true,
"mandatory": false,
"additionalFieldId": "label",
"maxCharacterLimit": 22,
"additionalFieldType": "TEXT",
"colorHexOptionsList": [],
"selectOptions": [] <-- new field
},
{
"label": "test-label-2",
"enabled": true,
"mandatory": false,
"additionalFieldId": "label2",
"maxCharacterLimit": 55,
"additionalFieldType": "TEXT",
"colorHexOptionsList": [],
"selectOptions": [] <-- new field
}
]
}
}
]
}
I have written the following SQL to try update the JSON payload within my table:
select
id,
payload,
row_version,
jsonb_set(
payload,
'{slots}',
(select
jsonb_agg(
jsonb_set(
slot_elem,
'{bannerXCreatorSettings}',
jsonb_set(
slot_elem -> 'bannerXCreatorSettings',
'{additionalFields}',
(
select
jsonb_agg(
jsonb_set(
field_element,
'{selectOptions}',
jsonb_build_array()))
from jsonb_array_elements(
slot_elem -> '{bannerXCreatorSettings}' #>'{additionalFields}') WITH ORDINALITY a_t(field_element, idx_1))
)
)
)
FROM
jsonb_array_elements(
payload #> '{slots}') WITH ORDINALITY t(slot_elem, idx))) as payload_update
My understanding of SQL is limited, however, I feel as though this should work. Unfortunally the above query results in the following:
{
"slots": [
null,
null
]
}

How to increment a counter inside a reduce function using dataweave

I am trying to do a JSON to JSON transformation using dataweave (for an EDI 811 mapping). My requirement is for every VEH segment (having Vehicle information) inside an HL loop, I want to start a counter from 1 to number of Vehicle records.
My input format is -
[
{
"IDENTIFIER": "ABC",
"HOST_ID": "XYZ",
"BusinessKey": "11111",
"HL0": {
"HL0_STATE_ID": "22",
"HL0_RPT_SYS_ID": "ABCD",
"HL0_CO_NAIC": "11111"
},
"HL1": {
"HL1_NAIC_COMP_CODE": "111111",
"HL1_CO_ADDRESS": "AAAA BBBB CCCCC",
"HL1_CO_CITY": "MABCD",
"HL1_CO_STATE": "AB",
"HL1_CO_ZIP_9": "12345"
},
"Details": [
{
"HL4": {
"HL4_POLICY_NUM": "8767886857",
"HL4_TRAN_CODE_ALPHA": "JGJ",
"HL4_CYCLE_DATE_CC": "19",
"HL4_CYCLE_DATE_YYMMDD": "200511",
"HL4_STATE_CODE": "VV",
"HL4_STATE_REPORTING_ID": "0000",
"HL4_PRODUCT_CODE": "DD",
"HL4_SUB_PRODUCT_CODE": "",
"HL4_SENDER_ID": "",
"HL4_POLICY_TYPE": "V",
"HL4_PROTEUS_TRANSACTION": "00000",
"HL4_TRAN_SUB_TYPE_CODE": "",
"HL4_TRAN_DATE_CC": "00",
"HL4_TRAN_DATE_YYMMDD": "000000",
"HL4_TRAN_TIME": "00000000",
"HL4_POL_EFF_DATE_CC": "00",
"HL4_POL_EFF_DATE_YYMMDD": "000000",
"HL4_POL_EXP_DATE_CC": "00",
"HL4_POL_EXP_DATE_YYMMDD": "000000",
"HL4_PERSON_ORG_INDIC": "1",
"HL4_LIAB_INDIC": "",
"HL4_AGENT_CODE": {
"HL4_AGENT_PREFIX": "",
"HL4_PRODUCER_AGENT": "00000"
},
"HL4_CANCEL_EFF_DATE": {
"HL4_CANCEL_EFF_DATE_CC": "00",
"HL4_XCL_EFF_DATE_YYMMDD": "000000"
},
"HL4_DRV_NAME": {
"HL4_DRV_LAST_NAME": "DDDDDD",
"HL4_DRV_FIRST_NAME_MI": "PPPPP"
},
"HL4_DRV_ADDRESS": "KHHKHK HJHKHKH GGGGGG RD S",
"HL4_DRV_CITY": "IOHKHL",
"HL4_DRV_STATE": "MB",
"HL4_DRV_ZIP": "68686",
"HL4_DRV_DLN": "875758865",
"HL4_DRV_DLN_STATE": "BJ",
"HL4_DRV_DOB_NUM": {
"HL4_DRV_DOB_YYMMDD": "20",
"HL4_DRV_DOB_CC": "200525"
},
"HL4_DRV_SEX": "",
"HL4_VEH_EFF_DATE_NUM": {
"HL4_VEH_EFF_DATE_CC": "00",
"HL4_VEH_EFF_DATE_YYMMDD": "000000"
},
"HL4_VEH_EXP_DATE_NUM": {
"HL4_VEH_EXP_DATE_CC": "20",
"HL4_VEH_EXP_DATE_YYMMDD": "220423"
}
},
"HL5": [
{
"HL5_VEH_VIN": "1BJHFJBKHJK6876868",
"HL5_VEH_MAKE": "CDRJV",
"HL5_VEH_CC": "19",
"HL5_VEH_YY": "99"
},
{
"HL5_VEH_VIN": "1BVNVJH68686JHG979",
"HL5_VEH_MAKE": "JHGJGJG",
"HL5_VEH_CC": "19",
"HL5_VEH_YY": "92"
}
]
},
{
"HL4": {
"HL4_POLICY_NUM": "6877578787",
"HL4_TRAN_CODE_ALPHA": "ABC",
"HL4_CYCLE_DATE_CC": "20",
"HL4_CYCLE_DATE_YYMMDD": "210805",
"HL4_STATE_CODE": "30",
"HL4_STATE_REPORTING_ID": "0000",
"HL4_PRODUCT_CODE": "CA",
"HL4_SUB_PRODUCT_CODE": "",
"HL4_SENDER_ID": "",
"HL4_POLICY_TYPE": "V",
"HL4_PROTEUS_TRANSACTION": "00000",
"HL4_TRAN_SUB_TYPE_CODE": "",
"HL4_TRAN_DATE_CC": "00",
"HL4_TRAN_DATE_YYMMDD": "000000",
"HL4_TRAN_TIME": "00000000",
"HL4_POL_EFF_DATE_CC": "00",
"HL4_POL_EFF_DATE_YYMMDD": "000000",
"HL4_POL_EXP_DATE_CC": "00",
"HL4_POL_EXP_DATE_YYMMDD": "000000",
"HL4_PERSON_ORG_INDIC": "2",
"HL4_LIAB_INDIC": "",
"HL4_AGENT_CODE": {
"HL4_AGENT_PREFIX": "",
"HL4_PRODUCER_AGENT": "00000"
},
"HL4_CANCEL_EFF_DATE": {
"HL4_CANCEL_EFF_DATE_CC": "00",
"HL4_XCL_EFF_DATE_YYMMDD": "000000"
},
"HL4_DRV_NAME": {
"HL4_DRV_LAST_NAME": "EE SSSSSS LLC",
"HL4_DRV_FIRST_NAME_MI": ""
},
"HL4_DRV_ADDRESS": "6869800 CCCCCC DR",
"HL4_DRV_CITY": "JFJJFJG",
"HL4_DRV_STATE": "GK",
"HL4_DRV_ZIP": "868875",
"HL4_DRV_DLN": "77797968123",
"HL4_DRV_DLN_STATE": "NM",
"HL4_DRV_DOB_NUM": {
"HL4_DRV_DOB_YYMMDD": "20",
"HL4_DRV_DOB_CC": "210907"
},
"HL4_DRV_SEX": "",
"HL4_VEH_EFF_DATE_NUM": {
"HL4_VEH_EFF_DATE_CC": "20",
"HL4_VEH_EFF_DATE_YYMMDD": "220410"
},
"HL4_VEH_EXP_DATE_NUM": {
"HL4_VEH_EXP_DATE_CC": "00",
"HL4_VEH_EXP_DATE_YYMMDD": "000000"
}
},
"HL5": [
{
"HL5_VEH_VIN": "1GJHHKHGJGJGJG878",
"HL5_VEH_MAKE": "JGKJGJH",
"HL5_VEH_CC": "20",
"HL5_VEH_YY": "15"
},
{
"HL5_VEH_VIN": "1JHGJGGKG97968687",
"HL5_VEH_MAKE": "GJJG",
"HL5_VEH_CC": "20",
"HL5_VEH_YY": "22"
},
{
"HL5_VEH_VIN": "186876BJHGJGJ7868",
"HL5_VEH_MAKE": "GJGKHKH",
"HL5_VEH_CC": "20",
"HL5_VEH_YY": "20"
}
]
}
]
}
]
My Output format JSON is as below, the VEH segments are all mapped from HL5 records in the above JSON input. in VEH01, I want to map the index/occurrence of HL5 record, so if there are 3 HL5 records with the HL4, we will get HL-VEH loop 3 times, and the VEH01 should increment from '1' for first occurrence, '2' for second occurrence and '3' for 3rd occurrence, For the next HL4-HL5 records, VEH01 should again start from "1"-
{
"TransactionSets": {
"v003050": {
"811": [
{
"Group": {
"GS04": "..."
},
"Heading": {
"020_BIG": {
"BIG01": "...",
"BIG02": ".."
},
....
....
"Detail": {
"010_HL_Loop": [
{
"010_HL": {
"HL01": "1",
"HL03": "1",
"HL04": "1"
},
...
...
{
"010_HL": {
"HL01": "8",
"HL02": "7",
"HL03": "5",
"HL04": "0"
},
"020_LX_Loop": [
{
"020_LX": {
"LX01": 1
},
"025_VEH": {
"VEH01": 1, ---> This should be 1
"VEH02": "1BJHFJBKHJK6876868",
"VEH03": ..,
"VEH04": ..,
"VEH05": "..",
"VEH06": "CDRJV"
}
}
]
},
{
"010_HL": {
"HL01": "9",
"HL02": "7",
"HL03": "5",
"HL04": "0"
},
"020_LX_Loop": [
{
"020_LX": {
"LX01": 1
},
"025_VEH": {
"VEH01": 1, ---> This should be 2
"VEH02": "1BVNVJH68686JHG979",
"VEH03": ..,
"VEH04": ..,
"VEH05": "..",
"VEH06": "JHGJGJG"
}
}
]
},
...
...
...
"010_HL": {
"HL01": "11",
"HL02": "10",
"HL03": "5",
"HL04": "0"
},
"020_LX_Loop": [
{
"020_LX": {
"LX01": 1
},
"025_VEH": {
"VEH01": 1, ---> This should be 1
"VEH02": "1GJHHKHGJGJGJG878",
"VEH03": ..,
"VEH04": ..,
"VEH05": "..",
"VEH06": "JGKJGJH"
}
}
]
},
{
"010_HL": {
"HL01": "12",
"HL02": "10",
"HL03": "5",
"HL04": "0"
},
"020_LX_Loop": [
{
"020_LX": {
"LX01": 1
},
"025_VEH": {
"VEH01": 1, ---> This should be 2
"VEH02": "1JHGJGGKG97968687",
"VEH03": ..,
"VEH04": ..,
"VEH05": "..",
"VEH06": "GJJG"
}
}
]
},
{
"010_HL": {
"HL01": "13",
"HL02": "10",
"HL03": "5",
"HL04": "0"
},
"020_LX_Loop": [
{
"020_LX": {
"LX01": 1
},
"025_VEH": {
"VEH01": 1, ---> This should be 3
"VEH02": "186876BJHGJGJ7868",
"VEH03": ..,
"VEH04": .,
"VEH05": "..",
"VEH06": "GJGKHKH"
}
}
]
}
]
My mapping in dataweave is as below -
%dw 2.0
import * from dw::core::Strings
output application/json
fun toHL1(rec, HL1Hierarchy) = {
"010_HL": {
HL01: (HL1Hierarchy + 1) as String,
HL03: "1",
HL04: "1"
},
"110_NM1_Loop": {
"110_NM1": {
NM101: "..",
NM102: "..",
NM103: "...",
NM108: "..",
NM109: rec.HL1.HL1_NAIC_COMP_CODE,
}
},
"210_IT1_Loop": [{
"210_IT1": {
IT102: 1,
IT103: "..",
IT104: 0
},
"270_DTM": [{
DTM01: "368",
DTM02: rec.Details.HL4.HL4_CYCLE_DATE_YYMMDD[0] as Date {
format: "yyMMdd"
} default "000000",
DTM05: rec.Details.HL4.HL4_CYCLE_DATE_CC[0] as Number
}]
}]
}
//////********************** */
fun toOrder(rec, shipHierarchy) = {
"010_HL": {
HL01: (shipHierarchy + 1) as String,
HL02: "1",
HL03: "2",
HL04: "1"
},
"110_NM1_Loop": {
"110_NM1": {
NM101: "..",
NM102: "..",
NM103: "..",
}
}
}
//////************************ */
fun toPack(ship, cases, ordHierarchy, indexOfPacks) = {
"010_HL": {
HL01: (indexOfPacks + 1) as String,
HL02: ordHierarchy as String,
HL03: "4",
HL04: "1",
},
"110_NM1_Loop": {
"110_NM1": {
NM101: "..",
NM102: cases.HL4.HL4_PERSON_ORG_INDIC,
NM103: cases.HL4.HL4_DRV_NAME.HL4_DRV_LAST_NAME,
NM104: cases.HL4.HL4_DRV_NAME.HL4_DRV_FIRST_NAME_MI,
NM108: "..",
NM109: cases.HL4.HL4_DRV_DLN
},
"130_N3": [{
N301: cases.HL4.HL4_DRV_ADDRESS
}],
"140_N4": {
N401: cases.HL4.HL4_DRV_CITY,
N402: cases.HL4.HL4_DRV_STATE,
N403: cases.HL4.HL4_DRV_ZIP
}
},
"210_IT1_Loop": [{
"210_IT1": {
IT102: 1,
IT103: "..",
IT104: 0
},
"220_SI": [{
SI01: "..",
SI02: "..",
SI03: cases.HL4.HL4_TRAN_CODE_ALPHA
}],
"260_REF": [{
REF01: "IG",
REF02: cases.HL4.HL4_POLICY_NUM[0 to 7],
},
{
REF01: "XM",
REF02: cases.HL4.HL4_DRV_DLN_STATE,
},
{
REF01: "S3",
REF02: "V",
},
{
REF01: "DD",
REF03: cases.HL4.HL4_POLICY_NUM[8 to 8] default " " ++ cases.HL4.HL4_CYCLE_DATE_CC as String ++ cases.HL4.HL4_CYCLE_DATE_YYMMDD as String,
}],
"270_DTM": [{
DTM01: "222",
DTM02: cases.HL4.HL4_DRV_DOB_NUM.HL4_DRV_DOB_CC as Date {
format: "yyMMdd"
} default "000000",
DTM05: cases.HL4.HL4_DRV_DOB_NUM.HL4_DRV_DOB_YYMMDD as Number
},
{
DTM01: if (cases.HL4.HL4_TRAN_CODE_ALPHA == "...") "036" else "007",
DTM02: if (cases.HL4.HL4_TRAN_CODE_ALPHA == "...") cases.HL4.HL4_VEH_EXP_DATE_NUM.HL4_VEH_EXP_DATE_YYMMDD as Date { format: "yyMMdd" } default "000000" else cases.HL4.HL4_VEH_EFF_DATE_NUM.HL4_VEH_EFF_DATE_YYMMDD as Date { format: "yyMMdd" } default "000000",
DTM05: if (cases.HL4.HL4_TRAN_CODE_ALPHA == "...") cases.HL4.HL4_VEH_EXP_DATE_NUM.HL4_VEH_EXP_DATE_CC as Number else cases.HL4.HL4_VEH_EFF_DATE_NUM.HL4_VEH_EFF_DATE_CC as Number
}]
}]
}
///////************************ */
fun toItem(ship, recItems, packHierarchy, indexOfItems) = {
"010_HL": {
// HL01: (4) as String,
HL01: (indexOfItems + 1) as String,
HL02: packHierarchy as String,
HL03: "5",
HL04: "0"
// HL04: cases
},
"020_LX_Loop": [{
"020_LX": {
"LX01": 1
},
"025_VEH": {
"VEH01": 1,
"VEH02": recItems.HL5_VEH_VIN,
"VEH03": recItems.HL5_VEH_CC as Number,
"VEH04": recItems.HL5_VEH_YY as Number {format: "0000"},
"VEH05": "NA",
"VEH06": recItems.HL5_VEH_MAKE
}
}]
}
---
{
//Delimiters: "*>U~",
TransactionSets: {
v003050: {
"811": [{
Group: {
GS04: now() as Date {
format: "yyMMdd"
},
},
Heading: {
"020_BIG": {
BIG01: now() as Date {
format: "yyMMdd"
},
BIG02: "1",
},
"100_N1_Loop": [{
"100_N1": {
N101: "..",
N102: "...",
N103: "..",
N104: "..."
},
},
{
"100_N1": {
N101: "..",
N102: "...",
}
}]
},
Detail: {
"010_HL_Loop": payload reduce (ship, accShip=[]) -> do {
var shipUpd = accShip << toHL1(ship, sizeOf(accShip))
var HL1Hierarchy = sizeOf(shipUpd)
var itemHierarchy = sizeOf(shipUpd)
var ord = payload reduce (ship, accOrd = shipUpd) -> do {
var ordUpd = accOrd << toOrder(ship, HL1Hierarchy)
var ordHierarchy = sizeOf(ordUpd)
var pack = flatten(payload.Details) reduce (cases, accPkg = ordUpd) -> do {
var packUpd = accPkg << toPack(ship, cases, ordHierarchy, sizeOf(accPkg))
var packHierarchy = sizeOf(packUpd)
var items = cases.HL5 reduce (recItems, accItems = packUpd) ->
accItems << toItem(ship, recItems, packHierarchy, sizeOf(accItems))
---
items
}
---
pack
}
---
ord
}
},
Summary: {
"010_TDS": {
TDS01: 1
},
"110_CTT": {
CTT01: sizeOf(flatten(payload.Details).HL5)
}
}
}]
}
}
}
Can someone please help with the increment and resetting of the counter for VEH01 for every H4-H5 loop.
If you are using reduce then the input is an array. You can use map() to add the index to each element of the array, then use as needed.
Since the transformation of the question is too complex to attempt to modify I'll make an educated guess to make a simple example:
%dw 2.0
output application/json
---
payload.HL5 map (($) ++ {index: $$ + 1})
Input:
{
"HL5": [
{
"HL5_VEH_VIN": "1GJHHKHGJGJGJG878",
"HL5_VEH_MAKE": "JGKJGJH",
"HL5_VEH_CC": "20",
"HL5_VEH_YY": "15"
},
{
"HL5_VEH_VIN": "1JHGJGGKG97968687",
"HL5_VEH_MAKE": "GJJG",
"HL5_VEH_CC": "20",
"HL5_VEH_YY": "22"
},
{
"HL5_VEH_VIN": "186876BJHGJGJ7868",
"HL5_VEH_MAKE": "GJGKHKH",
"HL5_VEH_CC": "20",
"HL5_VEH_YY": "20"
}
]
}
Output:
[
{
"HL5_VEH_VIN": "1GJHHKHGJGJGJG878",
"HL5_VEH_MAKE": "JGKJGJH",
"HL5_VEH_CC": "20",
"HL5_VEH_YY": "15",
"index": 1
},
{
"HL5_VEH_VIN": "1JHGJGGKG97968687",
"HL5_VEH_MAKE": "GJJG",
"HL5_VEH_CC": "20",
"HL5_VEH_YY": "22",
"index": 2
},
{
"HL5_VEH_VIN": "186876BJHGJGJ7868",
"HL5_VEH_MAKE": "GJGKHKH",
"HL5_VEH_CC": "20",
"HL5_VEH_YY": "20",
"index": 3
}
]
You need to identify at which point you need to apply the transformation and use the added index attribute. Maybe in between this expression: var items = cases.HL5 /* here? */ reduce ...

Karate: I get missing property in path $['data'] while using json filter path

I have gone through karate documentation and questions asked on stack overflow. There are 2 json arrays under resp.response.data. I am trying to retrieve and assert "bId": 81 in below json from the resp.response.data[1] but I get this missing property error while retrieving id value 81. Could you please help if I am missing something ?
* def resp =
"""
{
"response": {
"data": [
{
"aDetails": {
"aId": 15,
"aName": "Test",
"dtype": 2
},
"values": [
{
"bId": 45,
"value": "red"
}
],
"mandatory": false,
"ballId": "1231231414"
},
{
"aDetails": {
"aId": 25,
"aName": "Description",
"dtype": 2
},
"values": [
{
"bId": 46,
"value": "automation"
},
{
"bId": 44,
"value": "NESTED ARRAY"
},
{
"bId": 57,
"value": "sfERjuD"
},
{
"bId": 78,
"value": "zgSyPdg"
},
{
"bId": 79,
"value": "NESTED ARRAY"
},
{
"bId": 80,
"value": "NESTED ARRAY"
},
{
"bId": 81,
"value": "NESTED ARRAY"
}
],
"mandatory": true,
"ballId": "1231231414"
}
],
"corId": "wasdf-242-efkn"
}
}
"""
* def expectedbID=81
* def RespValueId = karate.jsonPath(resp, "$.data[1][?(#.bId == '" + expectedbID + "')]")
* match RespValueId[0] == expectedbID
Maybe you are over-complicating things ?
* match resp.response.data[1].values contains { bId: 81, value: 'NESTED ARRAY' }

Karate: Get all users and delete with the reference id

After my tests are completed, I would like to delete all the users that were created. I am able to delete one user at a time. Can we do this using conditional logic?
Feature file:
Given path 'users'
And header Authorization = authId
And header Accept = 'application/json;version=2'
When method Get
Then status 200
Then print 'Response Time: '+ responseTime + ' milliseconds'
* def resp = $
* def size = karate.sizeOf(resp)
* print 'Number of users: ' + size
This gives me below response:
[
{
"firstName": "Edit User",
"lastName": "API Test",
"emailAddress": "edituserapitesting#gmail.com",
"ada": true,
"isDeleted": false,
"alerts": [],
"links": [
{
"href": "https://someurl/api/users/0219360d-5ca6-42af-9e9c-10be1e32d219",
"rel": "self"
}
]
},
{
"firstName": "Create",
"lastName": "Test",
"emailAddress": "api#test.com",
"ada": true,
"isDeleted": false,
"alerts": [],
"links": [
{
"href": "https://someurl/api/users/5e3c9be1-2863-4a2d-85e9-966582b127ac",
"rel": "self"
}
]
},
{
"firstName": "Create",
"lastName": "Test",
"emailAddress": "api#test.com",
"ada": true,
"isDeleted": false,
"alerts": [],
"links": [
{
"href": "https://someurl/api/users/b6feb126-bca2-43e6-ba2d-87aaae81fef0",
"rel": "self"
}
]
},
{
"firstName": "Create",
"lastName": "Test",
"emailAddress": "api#test.com",
"ada": true,
"isDeleted": false,
"alerts": [],
"links": [
{
"href": "https://someurl/api/users/4f07f234-9606-4cf5-94ce-3a42fb11a6d7",
"rel": "self"
}
]
}
]
I have to get the id [Eg:4f07f234-9606-4cf5-94ce-3a42fb11a6d7] from each json and pass it to delete path.
Please read about transforms: https://github.com/intuit/karate#json-transforms
Now in one line you can get an array of ids:
* def ids = karate.map(response, function(x){ var link = x.links[0].href; return link.substring(link.lastIndexOf('/') + 1) })
Now you can use this in a loop or data driven test and do what you want.

How to traverse thru a response when it is as below

{
"createDate": "2019-05-15 10:07:44",
"mak": "pokijoklm",
"optStatus": "yujuim",
"partnerAccount": {
"operatorName": "frftcrtii",
"partnerCustomerId": "XXXXX",
"type": "partnerAccount"
},
"transactionId": "hjknhijn1",
"type": "nihnui",
"updateDate": "2019-05-15 11:20:59"
}
For an array we can try as response.partneraccount[*]. but my response is { }
You use [] only when there are arrays. Just observe your JSON structure carefully and you will get it. Try pasting the below into a fresh Scenario and see it work:
* def response =
"""
{
"createDate": "2019-05-15 10:07:44",
"mak": "pokijoklm",
"optStatus": "yujuim",
"partnerAccount": {
"operatorName": "frftcrtii",
"partnerCustomerId": "XXXXX",
"type": "partnerAccount"
},
"transactionId": "hjknhijn1",
"type": "nihnui",
"updateDate": "2019-05-15 11:20:59"
}
"""
* def partnerAccount = response.partnerAccount
* match partnerAccount == { operatorName: 'frftcrtii', partnerCustomerId: 'XXXXX', type: 'partnerAccount' }
* def custId = response.partnerAccount.partnerCustomerId
* match custId == 'XXXXX'