I would like compare two arrays (like input2) with numbers. And If the price is different about 5% increase, then I would like send email. I tried like this but it gave only the number that matches the same numbers. But I would like have only this price that it's increase 5% more.
%dw 2.0
output application/json
var input2 = [{
"T": "GIK",
"c": 10.02
},
{
"T": "EXTR",
"c": 4.43
},
{
"T": "TANH",
"c": 2.34
},
{
"T": "VSAT",
"c": 34.24
},
{
"T": "KIMpL",
"c": 25.9102
}]
---
payload.results.c reduce (item, acc = []) -> if (input2.c contains item) acc + item else acc
It appears from your script that you want the numbers (attribute c of input2 elements) that deviate a percentage from same attribute c from the payload. If you just need to compare the percentage from every number to every number in the other array this script does it. I used a function to calculate the percentage difference.
%dw 2.0
output application/json
import * from dw::core::Arrays
var maxPercentageDifference=5
fun percentageDifference(a,b)=abs(a-b)/((a+b)/2)*100
var input2 = [
{
"T": "GIK",
"c": 10.02
},
{
"T": "EXTR",
"c": 4.43
},
{
"T": "TANH",
"c": 2.34
},
{
"T": "VSAT",
"c": 34.24
},
{
"T": "KIMpL",
"c": 25.9102
}
]
---
payload.results.c
reduce (item, acc = []) -> if (input2.c some (percentageDifference(item, $) <= maxPercentageDifference)) acc + item else acc
Input:
{
"results": [
{
"T": "GIK",
"c": 10.05
},
{
"T": "EXTR",
"c": 4.41
},
{
"T": "TANH",
"c": 8
},
{
"T": "VSAT",
"c": 44
},
{
"T": "KIMpL",
"c": 25.9102
}
]
}
Output:
[
10.05,
4.41,
25.9102
]
However if you want to match the ids in attribute "T" before comparing the values, just change above script's body to:
payload.results
filter ((item, index) -> percentageDifference((input2 firstWith($.T == item.T)).c, item.c) <= maxPercentageDifference )
New Output:
[
{
"T": "GIK",
"c": 10.05
},
{
"T": "EXTR",
"c": 4.41
},
{
"T": "KIMpL",
"c": 25.9102
}
]
Related
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)
}
How to join 3 arrays by index (lines, pckSlip and linesDt) and generate an arrays object by linesDt after that you have to generate a new field "totalCost" which is to add the "cost" field of all cost elements in the linesDt array, note the "number" field in the original object is preserved for all new elements spawned from the parent.
Input:
{
"lines":[
{
"requestedQuantity":1,
"pendingQuantity":0
},
{
"requestedQuantity":2,
"pendingQuantity":0
}
],
"number":"98",
"pckSlip":[
{
"trackingNumber":"10534",
"boxesNum":1
},
{
"trackingNumber":"00049",
"boxesNum":1
}
],
"linesDt":[
{
"number":"5678",
"cost":7.7
},
{
"number":"1234",
"cost":7.3
}
]
}
Ouput:
[
{
"number":"5678",
"cost":7.7,
"requestedQuantity":1,
"pendingQuantity":0,
"trackingNumber":"10534",
"boxesNum":1,
"totalCost":15,
"order":"98"
},
{
"number":"1234",
"cost":7.3,
"requestedQuantity":2,
"pendingQuantity":0,
"trackingNumber":"00049",
"boxesNum":1,
"totalCost":15,
"order":"98"
}
]
NOTE: We generate 2 new elements because they are the total of indices found in "linesDt" within an array of elements.
Any help would be appreciated. Thank you.
Mapping each element of lines gives you the index to use in the other arrays. The ++ operator can be used to concatenate objects all the objects together. The calculated fields are added just as another object.
%dw 2.0
output application/json
var totalCost = sum(payload.linesDt.*cost)
---
payload.lines map (
$
++ payload.pckSlip[$$]
++ payload.linesDt[$$]
++ {totalCost: totalCost, order: payload.number}
)
Output:
[
{
"requestedQuantity": 1,
"pendingQuantity": 0,
"trackingNumber": "10534",
"boxesNum": 1,
"number": "5678",
"cost": 7.7,
"totalCost": 15.0,
"order": "98"
},
{
"requestedQuantity": 2,
"pendingQuantity": 0,
"trackingNumber": "00049",
"boxesNum": 1,
"number": "1234",
"cost": 7.3,
"totalCost": 15.0,
"order": "98"
}
]
Assuming that the size of each of the arrays is going to be the same.
Script
%dw 2.0
output application/json
---
1 to sizeOf(payload.lines) map {
(payload.linesDt[($$)] ++ payload.lines[($$)] ++ payload.pckSlip[($$)] ++ ("totalCost": sum(payload.linesDt..cost) as String) ++ ("order": payload.number))
}
Output
[
{
"number": "5678",
"cost": 7.7,
"requestedQuantity": 1,
"pendingQuantity": 0,
"trackingNumber": "10534",
"boxesNum": 1,
"totalCost": "15",
"order": "98"
},
{
"number": "1234",
"cost": 7.3,
"requestedQuantity": 2,
"pendingQuantity": 0,
"trackingNumber": "00049",
"boxesNum": 1,
"totalCost": "15",
"order": "98"
}
]
Data class
data class A(
var data: List<Data>
) {
data class Data(
var key: String,
var count: Long = 0,
var sub: List<Data>? = null
)
}
A class data values expressed in json.
[
{
"data": [
{
"key": "ALLOGENE THERAPEUTICS",
"count": 47,
"sub": [
{
"key": "N",
"count": 46,
"sub": [
{
"key": "S1",
"count": 1
},
{
"key": "S2",
"count": 13
}
]
},
{
"key": "B+",
"count": 1,
"sub": [
{
"key": "S1",
"count": 2
},
{
"key": "S2",
"count": 1
}
]
}
]
},
{
"key": "CELLECTIS",
"count": 5,
"sub": [
{
"key": "B+",
"count": 2,
"sub": [
{
"key": "S1",
"count": 3
},
{
"key": "S2",
"count": 5
}
]
},
{
"key": "B",
"count": 2,
"sub": [
{
"key": "S1",
"count": 6
},
{
"key": "S2",
"count": 1
}
]
},
{
"key": "N",
"count": 1,
"sub": [
{
"key": "S1",
"count": 8
},
{
"key": "S2",
"count": 4
}
]
}
]
},
{
"key": "PFIZER",
"count": 5,
"sub": [
{
"key": "N",
"count": 5,
"sub": [
{
"key": "S1",
"count": 83
},
{
"key": "S2",
"count": 1
}
]
}
]
}
]
}
]
I would like to combine elements with key values of "ALLOGENE THERAPEUTICS" and "CELECTIS" and replace the key value with "STUB".
When the elements are combined, all the "count" values must be combined.
And elements that are not there must be added.
Therefore, the results should be as follows.
[
{
"data": [
{
"key": "STUB",
"count": 52, // "ALLOGENE THERAPEUTICS"(47) + "CELECTIS"(5) = 52
"sub": [
{
"key": "N",
"count": 47, // 46 + 1
"sub": [
{
"key": "S1",
"count": 9
},
{
"key": "S2",
"count": 17
}
]
},
{
"key": "B+",
"count": 3,
"sub": [
{
"key": "S1",
"count": 5
},
{
"key": "S2",
"count": 6
}
]
},
{
"key": "B",
"count": 5,
"sub": [
{
"key": "S1",
"count": 11
},
{
"key": "S2",
"count": 7
}
]
}
]
},
{
"key": "PFIZER",
"count": 5,
"sub": [
{
"key": "N",
"count": 5,
"sub": [
{
"key": "S1",
"count": 83
},
{
"key": "S2",
"count": 1
}
]
}
]
}
]
}
]
How can I code the work neatly with Kotlin?
For reference, the values of the data class are expressed as json, and the result value must be data class.
This is the progress so far:
create a function for Data that creates a merged copy
data class Data(
var key: String,
var count: Long = 0,
var sub: List<Data> = emptyList()
) {
fun mergedWith(other: Data): Data {
return copy(
count = count + other.count,
sub = sub + other.sub
)
}
}
fold the consolidation list into a single data item and add them back together.
val consolidatedKeys = listOf("ALLOGENE THERAPEUTICS", "CELECTIS")
val (consolidatedValues, nonconsolidatedValues) = a.data.partition { it.key in consolidatedKeys }
val consolidatedData = when {
consolidatedValues.isEmpty() -> emptyList()
else -> listOf(consolidatedValues.fold(A.Data("STUB", 0), A.Data::mergedWith))
}
val result = A(consolidatedData + nonconsolidatedValues)
And combine the sub-elements.
consolidatedData.forEach { x ->
x.sub
.groupBy { group -> group.key }
.map { A.Data(it.key, it.value.sumOf { c -> c.count }) }
}
This is the current situation.
In this way, elements with depth of 2 will work normally, but elements with depth of 3 will not be added.
For example, up to "N" below STUB is combined, but "S1" and "S2" below "N" are not combined.
Therefore, the current result is output in this way.
[
{
"data": [
{
"key": "STUB",
"count": 52, <--------- WORK FINE
"sub": [
{
"key": "N",
"count": 47, <--------- WORK FINE
"sub": [] <--------- EMPTY !!
},
{
"key": "B+",
"count": 3, <--------- WORK FINE
"sub": [] <--------- EMPTY !!
},
{
"key": "B",
"count": 5, <--------- WORK FINE
"sub": [] <--------- EMPTY !!
}
]
},
{
"key": "PFIZER",
"count": 5,
"sub": [
{
"key": "N",
"count": 5,
"sub": [
{
"key": "S1",
"count": 83
},
{
"key": "S2",
"count": 1
}
]
}
]
}
]
}
]
How can all the sub-elements be combined and implemented?
First break down your problem. You can create a function for Data that creates a merged copy:
fun mergedWith(other: Data): Data {
return copy(
count = count + other.count,
sub = when {
sub == null && other.sub == null -> null
else -> sub.orEmpty() + other.sub.orEmpty()
}
)
}
I recommend if possible that you use a non-nullable List for your sub parameter, and use emptyList() when there's nothing in it. This makes it simpler since there aren't two different ways to represent a lack of items and you won't have to deal with nullability:
data class Data(
var key: String,
var count: Long = 0,
var sub: List<Data> = emptyList()
) {
fun mergedWith(other: Data): Data {
return copy(
count = count + other.count,
sub = sub + other.sub
)
}
}
Then you can split your list into ones that you want to consolidate vs. the rest. Then fold the consolidation list into a single data item and add them back together.
val consolidatedKeys = listOf("ALLOGENE THERAPEUTICS", "CELECTIS")
val (consolidatedValues, nonconsolidatedValues) = a.data.partition { it.key in consolidatedKeys }
val consolidatedData = when {
consolidatedValues.isEmpty() -> emptyList()
else -> listOf(consolidatedValues.fold(A.Data("STUB", 0), A.Data::mergedWith))
}
val result = A(consolidatedData + nonconsolidatedValues)
I am trying to transform an xml based on groupBy in dataweave, however I also need to remove few json attributes from output.
Input json:
[ {
"m": {
"a": "a",
"b": "b"
},
"tag1": "A",
"tag2": "v1",
"tag3": "v1" }, {
"m": {
"a": "a",
"b": "b"
},
"tag1": "A",
"tag2": "v2",
"tag3": "v2" }, {
"m": {
"a": "a",
"b": "b"
},
"tag1": "C",
"tag2": "v3",
"tag3": "v3" } ]
Output json
**{
"A": [
{
"tag2": "v1",
"tag3": "v1"
},
{
"tag2": "v2",
"tag3": "v2"
}
],
"C": {
"tag2": "v3",
"tag3": "v3"
}
}**
I have tried following transformation (Mule 3.9) however the could not remove the extra attributes in json.
payload groupBy (item) -> item.tag1
Appreciate any suggestion on this and possibly explain how this can be achieved.
The way to iterate over an object is using mapObject and then you can filter the objects to remove the elements that are not wanted
{a: [1,2,3], b: [2,3]} mapObject ((value,key) ->
{
(key): value filter ((value, index) -> value > 2)
}
)
This will output
{
"a": [
3
],
"b": [
3
]
}
I have following documents in azure cosmos db collection.
// Document 1
{
"c": {
"firstName": "Robert"
}
"elements" : [
{
"a": "x2",
"b": {
"name": "yadda2",
"id": 1
}
}
]
}
// Document 2
{
"c": {
"firstName": "Steve"
}
"elements" : [
{
"a": "x5",
"b": {
"name": "yadda2",
"id": 4
}
},
{
"a": "x3",
"b": {
"name": "yadda8",
"id": 5
}
},
]
}
// Document 3
{
"c": {
"firstName": "Johnson"
}
"elements" : [
{
"a": "x4",
"b": {
"name": "yadda28",
"id": 25
}
},
{
"a": "x5",
"b": {
"name": "yadda30",
"id": 37
}
},
]
}
I need to write a query that returns all documents which have "b" object whose name is "yadda2" (i.e. /elements/*/b/name=yadda2). In other words, this query should return document 1 and 2 but NOT 3.
I tried following but it did not work:
SELECT * FROM x where ARRAY_CONTAINS(x.elements, {b: { name: "yadda2"}})
What am I doing wrong?
Just modify your sql to :
SELECT * FROM x where ARRAY_CONTAINS(x.elements, {b: { name: "yadda2"}},true)
Result:
Based on the official doc , the boolean expression could specify if the match is full or partial.
Hope it helps you.