Convert array of json into single json with inner array - mule

I'm getting a message from a system in json format and want to perform a transformation in order to get below message
Source message:
[
{
"OPERATING_COMPANY": "xx",
"STORE_CLOSING_TIME": "2019-01-03T22:00:00",
"BATCH_ID": 1812,
"STORENUMBER": 1197,
"PROCESS_STATUS": "P"
},
{
"OPERATING_COMPANY": "xx",
"STORE_CLOSING_TIME": "2019-01-04T18:00:00",
"BATCH_ID": 1812,
"STORENUMBER": 1197,
"PROCESS_STATUS": "P"
},
{
"OPERATING_COMPANY": "xx",
"STORE_CLOSING_TIME": "2019-01-03T22:00:00",
"BATCH_ID": 1314,
"STORENUMBER": 1198,
"PROCESS_STATUS": "P"
},
{
"OPERATING_COMPANY": "xx",
"STORE_CLOSING_TIME": "2019-01-04T18:00:00",
"BATCH_ID": 1314,
"STORENUMBER": 1198,
"PROCESS_STATUS": "P"
}]
Expected Output
[
{
"OPERATING_COMPANY": "xx",
"STORE_CLOSING_TIME": ["2019-01-03T22:00:00","2019-01-04T18:00:00"],
"BATCH_ID": 1812,
"STORENUMBER": 1197,
"PROCESS_STATUS": "P"
},
{
"OPERATING_COMPANY": "xx",
"STORE_CLOSING_TIME": ["2019-01-03T22:00:00","2019-01-04T18:00:00"],
"BATCH_ID": 1812,
"STORENUMBER": 1198,
"PROCESS_STATUS": "P"
}]
I'm new to dataweave and tried using map but not able to understand how to achieve result.

Try the following in dataweave:
%dw 1.0
%output application/json
%var time=(payload map $.STORE_CLOSING_TIME) distinctBy $.STORE_CLOSING_TIME
---
(payload map {
"OPERATING_COMPANY": $.OPERATING_COMPANY,
"STORE_CLOSING_TIME": time,
"BATCH_ID": $.BATCH_ID,
"STORENUMBER": $.STORENUMBER,
"PROCESS_STATUS": $.PROCESS_STATUS
}) distinctBy $.STORENUMBER
output:
[
{
"OPERATING_COMPANY": "xx",
"STORE_CLOSING_TIME": [
"2019-01-03T22:00:00",
"2019-01-04T18:00:00"
],
"BATCH_ID": 1812,
"STORENUMBER": 1197,
"PROCESS_STATUS": "P"
},
{
"OPERATING_COMPANY": "xx",
"STORE_CLOSING_TIME": [
"2019-01-03T22:00:00",
"2019-01-04T18:00:00"
],
"BATCH_ID": 1314,
"STORENUMBER": 1198,
"PROCESS_STATUS": "P"
}
]

Related

How to get a desired output using groupBy in dataweave?

I'm looking for an output similar to this one below where i want to groupBy costomer and orderid.
Input:
[
{
"item": 621,
"orderid": "ON22",
"qty": 45.0,
"customer": "610",
"date": "1988-08-13"
},
{
"item": 63,
"orderid": "ON2234",
"qty": 7,
"customer": "813",
"date": "2001-08-13"
}
]
Desired output:
[
{
"customer":"813",
"data":[
{
"item":63,
"qty":7,
"orderid":"ON2234",
"date":"2001-08-13"
}
]
},
{
"customer":"610",
"data":[
{
"item": 621,
"qty": 45.0,
"orderid": "ON22",
"date": "1988-08-13"
}
]
}
]
You can simply map the default output of your groupBy result since your output does not require any additional logic.
%dw 2.0
output application/json
---
payload groupBy $.customer pluck ((customerOrders, customerId) -> {
customer: customerId as String,
data: customerOrders
})

Compare Object value with array in dataweave?

Sample input 1
{
"data": [
{
"a": [
{
"id": 123
}
],
"a1": [],
"a3": [],
"a4": []
},
{
"b": [
{
"bid": 133
}
],
"b1": [],
"b2": []
},
{
"c": [],
"c1": [],
"d": []
}
]
}
sample input 2: (based on which will filter the sample input 1)
[
"d",
"b",
"b1",
"a4"
]
by comparing the values of both the inputs
Scenario: based on the object names present in 2 input need to filter out the objects from the payload 1.
Expected final output:
{
"data": [{
"a": [{
"id": 123
}],
"a1": [],
"a3": []
},
{
"b2": []
},
{
"c": [],
"c1": []
}]
}
sample code:
%dw 2.0
output application/json
---
payload.data map ((item, index) -> item - "d" - "b" - "b1" - "a4") //
Note: This sample is working but but the values should be taken dynamically from the 2 input
Any help would be appreciated. Thank you.
Try with this:
Payload is the sample input 1 that you have typed in.
%dw 2.0
output application/json
var filterList = [ "d", "b", "b1", "a4" ]
---
data: payload.data map {
($ -- filterList)
}

Unable to access data in Postgres query where clause

I am dealing with the following JSON in a Postgres database column called steps in a table called tCampaign :
[
{
"name":"Step 1",
"stepReference":"01e9f7c0-bc79-11eb-ab6f-2fa1cb676e38",
"rewardConditions": [
{
"conditionDefinitions": [
{
"instanceId":"01805260-0818-4e99-e5b1-5820d1b133cd",
"type":"registration",
"properties": null,
"name": "Registration"
},
{
"instanceId":"01e115c3-5e56-437a-5d13-6c04281e9588",
"type":"optIn",
"properties": null,
"name":"Opt In"
}
],
"rewardDefinitions":[
{
"instanceId":"01c82190-1d56-44f9-474a-513732302e28",
"type":"sportsReward",
"properties": {"activation": {"type": "onReward"}, "betFlavour": "SPORTS", "channels": ["__use_campaign_restrictions__"], "expiry": {"offset": {"days": "02", "hours": "00", "minutes": "00", "seconds": "00"}, "type": "relative"}, "inRunning": "-", "maxReward": {"USD": "1"}, "minimumOdds": "", "oddsInput": {"minimumOdds": {"american": "", "european": ""}}, "retail": "offBetBuild", "returnStakeOnPayout": "false"},
"name":"Freebet",
"calculator":{"type":"fixed","value":"100"}
}
]
}
]
},
{
"name" : "Step 2",
"stepReference" : "01daa4a0-bc79-11eb-ab6f-2fa1cb676e38",
"rewardConditions": [
{
"conditionDefinitions": [
{
"instanceId" : "01fb15ae-01d0-49e1-966a-8ff438e9a191",
"type" : "genericSportsBet",
"properties" : {"betFlavour": "SPORTS", "betTrackEventThreshold": "10", "betTypes": [ "SGL" ], "builderBetOption": "ALL", "channels": [ "__use_campaign_restrictions__" ], "currencyThresholdMap": { "USD": "1" }, "eventHierarchySelection": { "categories": [], "classes": [], "events": [], "marketTemplates": [], "markets": [ "5824" ], "retrobetEventIds": [ "1200" ], "selections": [], "selectionsMarket": [], "types": [] }, "eventHierarchySelectionUI": { "markets": [ { "id": 5824, "mapping": [], "name": "Match Result", "parentId": 1200, "parentParentId": 5, "path": [ "Category: |England|", "Class: |England Premier League|", "Type: |GK Team K| |vs| |GK Team L|" ], "selectionMapper": false, "settled": "N", "startTime": "2021-05-31 11:15:00", "status": "A" } ] }, "inRunning": "-", "legTypes": [ "WIN" ], "metOnSettlement": false, "minOdds": "", "oddsInput": { "minOdds": { "american": "", "european": "" } }, "priceTypes": [ "LP" ]},
"name" : "Sports Bet"
}
],
"rewardDefinitions":[
{
"instanceId" : "0110eb70-44f9-4d57-40bb-09ff4169136c",
"type" : "sportsReward",
"properties" : {"activation": {"type": "onReward"}, "betFlavour": "SPORTS", "channels": ["__use_campaign_restrictions__"], "expiry": {"offset": {"days": "02", "hours": "00", "minutes": "00", "seconds": "00"}, "type": "relative"}, "inRunning": "-", "maxReward": {"USD": "2"}, "minimumOdds": "", "oddsInput": {"minimumOdds": {"american": "", "european": ""}}, "retail": "offBetBuild", "returnStakeOnPayout": "false"},
"name" : "Freebet",
"calculator" : {"type":"fixed","value":"100"}
}
]
}
]
}
]
and have written the following query to extract properties from conditionDefinitions :
select conditionDefinitions->'properties' as properties from tcampaign cmp
LEFT JOIN LATERAL json_array_elements(steps) singleStep ON true
LEFT JOIN LATERAL json_array_elements(singleStep->'rewardConditions') rewardConditions on TRUE
LEFT JOIN LATERAL json_array_elements(rewardConditions->'conditionDefinitions') conditionDefinitions on TRUE
where properties is not null ;
but I get the following error :
ERROR: column "properties" does not exist
LINE 5: where properties is null ;
If I remove the where clause the query runs fine. Why do I not have access to properties in the where clause? Because I can see results coming back if I remove the WHERE clause, so the query does have results

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

Unflatten an Array in Mulesoft Dataweave 2.0

I have a flat array of Customers, and could not find an appropriate Method (Way) to Unflatten it so that each Customer & his Age becomes Nested.
INPUT
{
"1st Customer": "2216",
"Age": "90",
"2nd Customer": "2231",
"Age": "90",
"3rd Customer": "2249",
"Age": "120",
"4th Customer": "2302",
"Age": "150",
"5th Customer": "",
"Age": ""
}
OUTPUT
{
"customers": [
{
"CustomerSeq": "1",
"CustomerID": "2216",
"Age": 90,
},
{
"CustomerSeq": "2",
"CustomerID": "2231",
"Age": 90,
},
{
"CustomerSeq": "3",
"CustomerID": "2249",
"Age": 120,
},
{
"CustomerSeq": "5",
"CustomerID": "2302",
"Age": 150,
}
]
}
Thanks a lot, Alex
The divideBy is exactly what I was looking for
The Solution [including the removal of the empty values]:
%dw 2.0
output application/json
import * from dw::core::Objects
---
customers:
((payload filterObject ((value, key, index) -> value != "")) divideBy 2) map (
(pCustomer, indexOfpCustomer) -> {
CustomerSeq:indexOfpCustomer+1,
CustomerID: pCustomer[0],
Age: pCustomer[1]
})
There is a good function for it in Mule: divideBy.
It will split an array of your values from the object, and then the requested object could be created:
%dw 2.0
var x={
"1st Customer": "2216",
"Age": "90",
"2nd Customer": "2231",
"Age": "90",
"3rd Customer": "2249",
"Age": "120",
"4th Customer": "2302",
"Age": "150",
"5th Customer": "",
"Age": ""
}
var keys=x pluck $$
var values=x pluck $
import * from dw::core::Arrays
output application/dw
---
values divideBy 2 map (item,index) ->
{
CustomerSeq:index+1,
CustomerID:item[0],
Age:item[1]
}
output
[
{
CustomerSeq: 1,
CustomerID: "2216",
Age: "90"
},
{
CustomerSeq: 2,
CustomerID: "2231",
Age: "90"
},
{
CustomerSeq: 3,
CustomerID: "2249",
Age: "120"
},
{
CustomerSeq: 4,
CustomerID: "2302",
Age: "150"
},
{
CustomerSeq: 5,
CustomerID: "",
Age: ""
}
]