Creating a resulting array with unique values in dataweave - mule

Need to compare two arrays efficiently and create a third array with values that are only in the second array using Dataweave transformation in mule. I wanted to use the negation of contains the keyword in mule. But it was giving errors. Hope that I can use a filter and Contains to filter out the values.
arr1 =[
{
"leadId": 127,
"playerId": 334353
},
{
"leadId": 128,
"playerId": 334354
},
{
"leadId": 123,
"playerId": 43456
}
{
"leadId": 122,
"playerId": 43458
}
arr2 =[
{
"leadId": 127,
"name": "James"
},
{
"leadId": 129,
"name": "Joseph"
},
{
"leadId": 120,
"name": "Samuel"
},
{
"leadId": 122,
"name": "Gabriel",
}
Need resulting array as
arr3 = [
{
"leadId": 129,
"name": Joseph
},
{
"leadId": 120,
"name": Samuel
}
]

UPDATED - Including DataWeave 1 and 2
I'm not sure exactly how you have your arrays stored (in the payload or other variables etc), but the below script should give you enough to go on. Comparison is done based on the leadId only.
The transform actually works across both DW1 and DW2, you just need to change the header.
Mule 3 and DW1
%dw 1.0
%output application/json
---
payload.array2 filter (not (payload.array1.leadId contains $.leadId))
Mule 4 and DW2
%dw 2.0
output application/json
---
payload.array2 filter (not (payload.array1.leadId contains $.leadId))
Input
{
"array1": [
{
"leadId": 127,
"playerId": 334353
},
{
"leadId": 128,
"playerId": 334354
},
{
"leadId": 123,
"playerId": 43456
},
{
"leadId": 122,
"playerId": 43458
}
],
"array2": [
{
"leadId": 127,
"name": "James"
},
{
"leadId": 129,
"name": "Joseph"
},
{
"leadId": 120,
"name": "Samuel"
},
{
"leadId": 122,
"name": "Gabriel"
}
]
}
Output
[
{
"leadId": 129,
"name": "Joseph"
},
{
"leadId": 120,
"name": "Samuel"
}
]

Related

JSON SQL column in azure data factory

I have a JSON type SQL column in SQL table as below example. I want the below code to be converted into separate columns such as drugs as table name and other attribute as column name, how can I use adf or any other means please guide. The below code is a single column in a table called report where I need to convert this into separate columns .
{
"drugs": {
"Codeine": {
"bin": "Y",
"name": "Codeine",
"icons": [
93,
103
],
"drug_id": 36,
"pathway": {
"code": "prodrug",
"text": "is **inactive**, its metabolites are active."
},
"targets": [],
"rxnorm_id": "2670",
"priclasses": [
"Analgesic/Anesthesiology"
],
"references": [
1,
16,
17,
100
],
"subclasses": [
"Analgesic agent",
"Antitussive agent",
"Opioid agonist",
"Phenanthrene "
],
"metabolizers": [
"CYP2D6"
],
"phenotype_ids": {
"metabolic": "5"
},
"relevant_genes": [
"CYP2D6"
],
"dosing_guidelines": [
{
"text": "Reduced morphine formation. Use label recommended age- or weight-specific dosing. If no response, consider alternative analgesics such as morphine or a non-opioid.",
"source": "CPIC",
"guidelines_id": 1
},
{
"text": "Analgesia: select alternative drug (e.g., acetaminophen, NSAID, morphine-not tramadol or oxycodone) or be alert to symptoms of insufficient pain relief.",
"source": "DPWG",
"guidelines_id": 362
}
],
"drug_report_notes": [
{
"text": "Predicted codeine metabolism is reduced.",
"icons_id": 58,
"sort_key": 58,
"references_id": null
},
{
"text": "Genotype suggests a possible decrease in exposure to the active metabolite(s) of codeine.",
"icons_id": 93,
"sort_key": 56,
"references_id": null
},
{
"text": "Professional guidelines exist for the use of codeine in patients with this genotype and/or phenotype.",
"icons_id": 103,
"sort_key": 50,
"references_id": null
}
]
}
Since this json is already in a SQL column, you don't need ADF to break it down to parts. You can use JSON functions in SQL server to do that.
example of few first columns:
declare #json varchar(max) = '{
"drugs": {
"Codeine": {
"bin": "Y",
"name": "Codeine",
"icons": [
93,
103
],
"drug_id": 36,
"pathway": {
"code": "prodrug",
"text": "is **inactive**, its metabolites are active."
},
"targets": [],
"rxnorm_id": "2670",
"priclasses": [
"Analgesic/Anesthesiology"
],
"references": [
1,
16,
17,
100
],
"subclasses": [
"Analgesic agent",
"Antitussive agent",
"Opioid agonist",
"Phenanthrene "
],
"metabolizers": [
"CYP2D6"
],
"phenotype_ids": {
"metabolic": "5"
},
"relevant_genes": [
"CYP2D6"
],
"dosing_guidelines": [
{
"text": "Reduced morphine formation. Use label recommended age- or weight-specific dosing. If no response, consider alternative analgesics such as morphine or a non-opioid.",
"source": "CPIC",
"guidelines_id": 1
},
{
"text": "Analgesia: select alternative drug (e.g., acetaminophen, NSAID, morphine-not tramadol or oxycodone) or be alert to symptoms of insufficient pain relief.",
"source": "DPWG",
"guidelines_id": 362
}
],
"drug_report_notes": [
{
"text": "Predicted codeine metabolism is reduced.",
"icons_id": 58,
"sort_key": 58,
"references_id": null
},
{
"text": "Genotype suggests a possible decrease in exposure to the active metabolite(s) of codeine.",
"icons_id": 93,
"sort_key": 56,
"references_id": null
},
{
"text": "Professional guidelines exist for the use of codeine in patients with this genotype and/or phenotype.",
"icons_id": 103,
"sort_key": 50,
"references_id": null
}
]
}
}
}
select JSON_VALUE(JSON_QUERY(#json,'$.drugs.Codeine'),'$.bin') as bin,
JSON_VALUE(JSON_QUERY(#json,'$.drugs.Codeine'),'$.name') as name,
JSON_VALUE(JSON_QUERY(#json,'$.drugs.Codeine'),'$.drug_id') as drug_id,
JSON_VALUE(JSON_QUERY(#json,'$.drugs.Codeine'),'$.icons[0]') as icon_1
'
You need to decide how to handle arrays, such as icons, where there are multiple values inside the same element.
References:
JSON_QUERY function
JSON_VALUE function

Mule 4 : Batch Processing : Error in Accept Expression in Batch step

I am using Batch processing which has more than one batch steps.
The output of one batch step is :
[
{
"CustomerId": "00",
"TotalPurchase": 0
},
{
"CustomerId": "11",
"TotalPurchase": 1
},
{
"CustomerId": "22",
"TotalPurchase": 8
},
{
"CustomerId": "33",
"TotalPurchase": 27
},
{
"CustomerId": "44",
"TotalPurchase": 64
},
{
"CustomerId": "55",
"TotalPurchase": 125
},
{
"CustomerId": "66",
"TotalPurchase": 216
},
{
"CustomerId": "77",
"TotalPurchase": 343
},
{
"CustomerId": "88",
"TotalPurchase": 512
},
{
"CustomerId": "99",
"TotalPurchase": 729
},
{
"CustomerId": "1010",
"TotalPurchase": 1000
}
]
In The next Batch step, I am using the ACCEPT EXPRESSION field with value as :
#[payload.TotalPurchase > 100]
But I am getting the error :
Types `Array` and `Number` can not be compared.
payload.TotalPurchase > 100
^^^^^^^^^^^^^^^^^^^^^
Any ideas why this is happening?
Maybe you want to process each element of the array as a record but for that input payload the value of #[payload.TotalPurchase] is:
[
0,
1,
8,
27,
64,
125,
216,
343,
512,
729,
1000
]
That's because DataWeave returns an array of all TotalPurchase members in the array. So it is not possible to compare that computed array to a number.

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

How to filter the Array of Dictionary if the dictionary elements are null?

I need to filter the array of dictionary , but in my structure some of the values are null
"data": [
{
"date": "2019-04-12 00:00:00",
"points": 825,
"trips": [
{
"id": 108,
"trip_detail": “<null>”
}
},
{
"date": "2019-04-11 00:00:00",
"points": 2475,
"trips": [
{
"id": 73,
"trip_detail": {
"id": 53,
"score": 8.25,
"points": 825,
}
},
{
"id": 74,
"trip_detail": {
"id": 54,
"score": 8.25,
"points": 825,
}
},
so in the above result I need to remove the first array of dictionary as well as that array.
Use filter to loop over a collection and return an Array containing only those elements that match an include condition.

Converting a Review API call to html

I am sorry to trouble.
I am unable to convert an API call into html that can go on a web page.
The call is like this:
https://api.feefo.com/api/version/reviews/summaryall?merchant_identifier=example-retail-merchant
And it produces something like this:
{
"merchant": {
"identifier": "example-retail-merchant",
"name": "Example Retail Merchant",
"url": "http://www.exampleretailmerchant.co.uk",
"logo": "example-retail-merchant-logo.png",
"review_url": "http://www.feefo.com/en_GB/reviews/example-retail-merchant"
},
"meta": {
"count": 3878,
"pages": 194,
"page_size": 20
},
"rating": {
"min": 1,
"max": 5,
"rating": 4.9,
"service": {
"count": 3878,
"1_star": 8,
"2_star": 28,
"3_star": 0,
"4_star": 181,
"5_star": 3661
},
"product": {
"count": 6240,
"1_star": 55,
"2_star": 102,
"3_star": 0,
"4_star": 724,
"5_star": 5359
}
}
}
Can anyone please point me in the right directoion?
Thank you so much...
According to jQuery, you can use the following function to make API calls:
$(document).ready( function() {
var info;
var whitelisted;
var quantity;
$.get("https://api.guildwars2.com/v2/commerce/prices/24615",function(obj){
info = obj['id'];
whitelisted = obj["whitlelisted"]
quantity = obj.buys['quantity']
$("#id1").html("id :"+info);
$("#whitelist").html("whitelisted :"+whitelisted);
$("#quan").html("quantity :"+quantity);
});
});