How to check multiple conditions in a loop using Data Weave 1 - mule

**I have below sample payload, where I need below conditions to meet and get the same payload back using DW 1.0
If isOwned = true for productId = 1003
Then show newPrice = null and hasSpecialPrice = false for 1004,1002,1001,1000
If isOwned = true for productId = 1004
Then show newPrice = null and hasSpecialPrice = false for 1002,1001,1000
If isOwned = true for productId = 1002
Then show newPrice = null and hasSpecialPrice = false for 1001,1000
**
{
"products":[
{
"flavors":[
{
"features":null,
"entitlement":{
"isOwned":false
},
"locale":"en_US",
"price":"0.00",
"newPrice":null,
"hasSpecialPrice":false,
"productId":"1000"
}
],
"id":"1000",
"enabled":null
},
{
"flavors":[
{
"features":null,
"entitlement":{
"isOwned":false
},
"locale":"en_US",
"price":"6.99",
"newPrice":null,
"hasSpecialPrice":false,
"productId":"1001"
}
],
"id":"1001",
"enabled":null
},
{
"flavors":[
{
"features":null,
"entitlement":{
"isOwned":false
},
"locale":"en_US",
"price":"5.99",
"newPrice":null,
"hasSpecialPrice":false,
"productId":"1002"
}
],
"id":"1002",
"enabled":null
},
{
"flavors":[
{
"features":null,
"entitlement":{
"isOwned":true
},
"locale":"en_US",
"price":"3.99",
"newPrice":null,
"hasSpecialPrice":false,
"productId":"1003"
}
],
"id":"1003",
"enabled":null
},
{
"flavors":[
{
"features":null,
"entitlement":{
"isOwned":false
},
"locale":"en_US",
"price":"2.99",
"newPrice":"1.99",
"hasSpecialPrice":true,
"productId":"1004"
}
],
"id":"1004",
"enabled":null
}
]
}
Expected Output for condition isOwned = true for productId = 1003 Then show values newPrice = null and hasSpecialPrice = false for 1004,1002,1001,1000:
{
"products":[
{
"flavors":[
{
"features":null,
"entitlement":{
"isOwned":false
},
"locale":"en_US",
"price":"0.00",
"newPrice":null,
"hasSpecialPrice":false,
"productId":"1000"
}
],
"id":"1000",
"enabled":null
},
{
"flavors":[
{
"features":null,
"entitlement":{
"isOwned":false
},
"locale":"en_US",
"price":"6.99",
"newPrice":null,
"hasSpecialPrice":false,
"productId":"1001"
}
],
"id":"1001",
"enabled":null
},
{
"flavors":[
{
"features":null,
"entitlement":{
"isOwned":false
},
"locale":"en_US",
"price":"5.99",
"newPrice":null,
"hasSpecialPrice":false,
"productId":"1002"
}
],
"id":"1002",
"enabled":null
},
{
"flavors":[
{
"features":null,
"entitlement":{
"isOwned":true
},
"locale":"en_US",
"price":"3.99",
"newPrice":null,
"hasSpecialPrice":false,
"productId":"1003"
}
],
"id":"1003",
"enabled":null
},
{
"flavors":[
{
"features":null,
"entitlement":{
"isOwned":false
},
"locale":"en_US",
"price":"2.99",
"newPrice":null,
"hasSpecialPrice":false,
"productId":"1004"
}
],
"id":"1004",
"enabled":null
}
]
}

In DataWeave 1.0 you can use when/otherwise for simple conditions and pattern matching with match for more complex ones. To avoid putting too much logic in the main map, I created a variable that returns which of the rules is triggered and a function that updates each element. The object updateWhich is for identifying which elements needs to be updated based on the id triggered.
%dw 1.0
%output application/json
%var updateWhich={
"1002": ["1000", "1001"],
"1003": ["1000", "1001", "1002", "1004"],
"1004": ["1000", "1001", "1002"]
}
%var updateId=(payload.products map ( $ match {
o when o.id == "1002" and o.flavors.entitlement.isOwned[0] -> "1002",
o when o.id == "1003" and o.flavors.entitlement.isOwned[0] -> "1003",
o when o.id == "1004" and o.flavors.entitlement.isOwned[0] -> "1004",
default -> null
}) filter ( $ != null ))[0]
%function updateProduct(o) o - "flavors" ++ {flavors: [o.flavors[0] - "newPrice" - "hasSpecialPrice" ++ {newPrice: null, hasSpecialPrice: false} ]}
---
products: payload.products map ( updateProduct($) when (updateWhich[updateId] != null) otherwise $)

Related

Query item in nested array

Customer appointments with top level locationId sample data set:
[
{
"locationId": 9999,
"customerAppointments": [
{
"customerId": "1",
"appointments": [
{
"appointmentId": "cbbce566-da59-42c2-8845-53976ba63d56",
"locationName": "Sullivan St"
},
{
"appointmentId": "5f09e2af-ddae-47aa-9f7c-fd1001a9c5e6",
"locationName": "Oak St"
}
]
},
{
"customerId": "2",
"appointments": [
{
"appointmentId": "964a3c1c-ccec-4082-99e2-65795352ba79",
"locationName": "Kellet St"
}
]
},
{
"customerId": "3",
"appointments": []
}
]
},
{
...
},
{
...
}
]
I need to pull out appointment by locationId and customerId and only get the appointment for that customerId e.g
Sample response:
[
{
"appointmentId": "964a3c1c-ccec-4082-99e2-65795352ba79",
"locationName": "Kellet St"
}
]
Tried below query, but it just returns all records for all customers ids (which is kind of expected):
db.getCollection("appointments").find(
{
"locationId" : NumberInt(9999),
"customerAppointments" : {
"$elemMatch" : {
"customerId" : "2"
}
}
}
);
But how can I get just the appointment record for a specific customerId?
When asking this question I was unaware of the older version of MongoDB driver (< v5) so we cannot use the $getField operator.
However, this query seems to work well:
db.getCollection("appointments").aggregate([
{
$match: {
"locationId": NumberInt(9999)
}
},
{
$unwind: "$customerAppointments"
},
{
$match: {
"customerAppointments.customerId": "2"
}
},
{
$project: {
appointments: "$customerAppointments.appointments"
}
}
]);
Yields:
{
"_id" : ObjectId("63eebe95c7a0da54804c1db2"),
"appointments" : [
{
"appointmentId" : "964a3c1c-ccec-4082-99e2-65795352ba79",
"locationName" : "Kellet St"
}
]
}

Parsing Dynamic response in Karate

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']

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 - Replace a value with if condition

I have the following json. I want to replace 'Türkiye' with 'Türkice' if a == "hello". Please help me with the guidance to add replace with the if condition combination
* def a = "hello"
* def expected =
"""
{
"_": {
"country-language-list": {
"country-language": [
{
"_": "English",
"#": {
"languageCode": "en"
}
},
{
"_": "Deutsch",
"#": {
"languageCode": "de"
}
},
{
"_": "Türkiye",
"#": {
"languageCode": "tr"
}
}
]
}
},
"#": {
"countryCode": "TR"
}
}
"""
Here you go:
* if (a == 'hello') expected._['country-language-list']['country-language'][2]._ = 'Türkice'

How can I define jsonPath for given json?

{
"name": "ninja",
"contry": "India",
"Account": [
{
"id": "123",
"orgId": 223,
"investment": [
{
"invetmentId": "111",
"name": "India tech",
"performance": [
{
"id": "123",
"performanceSet": [
{
"amount": "231",
"currency": "USD"
},
{
"amount": "250",
"currency": "IND"
}
]
}
]
}
]
}
]
}
So I have to select the amount where the currency is USD?
And I tried it as "$.Account..investment.performance..performanceSet.amount[?(#.currency=~/.*USD/)]"
This JsonPath should work:
$..performanceSet[?(#.currency == "USD")].amount
Tested on:
{
"name":"ninja",
"contry":"India",
"Account":[
{
"id":"123",
"orgId":223,
"investment":[
{
"invetmentId":"111",
"name":"India tech",
"performance":[
{
"id":"123",
"performanceSet":[
{
"amount":"231",
"currency":"USD"
},
{
"amount":"250",
"currency":"IND"
}
]
}
]
},
{
"invetmentId":"112",
"name":"India tech 2",
"performance":[
{
"id":"124",
"performanceSet":[
{
"amount":"235",
"currency":"USD"
},
{
"amount":"250",
"currency":"IND"
}
]
}
]
}
]
}
]
}
which returns:
[
"231",
"235"
]
A good way to try it out is this site: https://jsonpath.com/
Read the docs: https://github.com/intuit/karate#jsonpath-filters
* def temp = $..performanceSet[?(#.currency=='USD')]
* match temp[0].amount == '231'
You can try it this way
$.Account..investment.performance..performanceSet.amount[?(#.currency=~/.*USD/)]