How do i convert this code from Dataweave 1 to Dataweave 2 - mule-studio

I have to convert some code from dataweave 1 to dataweave 2 but I'm not sure how to convert this patch of code. The answers I found online are confusing
I'm not sure if these two maps are syntaxed the right way, or the syntax for filtering then mapping. I know you have to convert flowVars to vars
flowVars.referenceDataResponse.resultSets filter $.schemaSequenceNumber == "TRESIDENCE_TYPE" map (resultSets,indexOfResultSets) -> {
(resultSets.resultSet map (resultSet,indexOfResultSet) -> {
residenceType:resultSet[indexOfResultSet].data
} )
}

You should really give more context, like inputs, expected output and the directives of the script but this is a literal translation, with output set to application/java because of lack of anything else:
%dw 2.0
output application/java
---
vars.referenceDataResponse.resultSets filter $.schemaSequenceNumber == "TRESIDENCE_TYPE" map (resultSets, indexOfResultSets) -> {
(resultSets.resultSet map (resultSet, indexOfResultSet) -> {
residenceType: resultSet[indexOfResultSet].data
})
}
No way to know if it works for you, given that there is no test data.

Alejandro,
I have Posted sample data, may help to test your suggested conversion solution and I have assumed resultSet is an object with two attribute. You may improve the data to cater your suggested solution
%dw 2.0
output application/json
var arr =
referenceDataResponse: {
resultSets :
[
{
id: 1,
schemaSequenceNumber :"TRESIDENCE_TYPE",
resultSet: [{residenceType: "one"},{residenceType: "two"},{residenceType: "three"}]
},
{
id: 2,
schemaSequenceNumber :"TRESIDENCE_TYPE2",
resultSet:[{residenceType: "one"},{residenceType: "two"},{residenceType: "three"}]
},
{
id: 3,
schemaSequenceNumber :"TRESIDENCE_TYPE3",
resultSet:[{residenceType: "one"},{residenceType: "two"},{residenceType: "three"}]
}
]
}
---
arr.referenceDataResponse.resultSets filter $.schemaSequenceNumber == "TRESIDENCE_TYPE" map (resultSets, indexOfResultSets) -> {
(resultSets.resultSet map (resultSet, indexOfResultSet) -> {
residenceType: resultSet.residenceType
})
}

Related

How to edit a value (list of entries) from an api response to use in a request body in Gatling/Scala

I have an issue that I'm hoping someone can help me with. I'm pretty new to coding and Gatling, so I'm not sure how to proceed.
I'm using Gatling (with Scala) to create a performance test scenario that contains two API-calls.
GetInformation
SendInformation
I'm storing some of the values from the GetInformation response so I can use it in the body for the SendInformation request. The problem is that some information from the GetInformation response needs to be edited/removed before it is included in the body for SendInformation.
Extract of the GetInformation response:
{
"parameter": [
{
"name": "ResponseFromGetInfo",
"type": "document",
"total": 3,
"entry": [
{
"fullUrl": "urn:uuid:4ea859d0-daa4-4d2a-8fbc-1571cd7dfdb0",
"resource": {
"resourceType": "Composition"
}
},
{
"fullUrl": "urn:uuid:1b10ed79-333b-4838-93a5-a40d22508f0a",
"resource": {
"resourceType": "Practitioner"
}
},
{
"fullUrl": "urn:uuid:650b8e7a-2cfc-4b0b-a23b-a85d1bf782de",
"resource": {
"resourceType": "Dispense"
}
}
]
}
]
}
What I want is to store the list in "entry" and remove the entries with resourceType = "Dispense" so I can use it in the body for SendInformation.
It would have been ok if the entry list always had the same number of entries and order, but that is not the case. The number of entries can be several hundred and the order of entries varies. The number of entries are equal to the "total" value that is included in the GetInformation response.
I've thought about a few ways to solve it, but now I'm stuck. Some alternatives:
Extract the entire "entry" list using .check(jsonPath("$.parameter[0].entry").saveAs("entryList")) and then iterate through the list to remove the entries with resourceTypes = "Dispense".
But I don't know how to iterate over a value of type io.gatling.core.session.SessionAttribute, or if this is possible. It would have been nice if I could iterate over the entry list and check if parameter[0].entry[0].resourceType = "Dispense", and remove the entry if the statement is true.
I'm also considering If I can use StringBuilder in some way. Maybe if I check one entry at the time using .check(parameter[0].entry[X].resourceType != dispense, and if true then append it to a stringBuilder.
Does someone know how I can do this? Either by one of the alternatives that I listed, or in a different way? All help is appreciated :)
So maybe in the end it will look something like this:
val scn = scenario("getAndSendInformation")
.exec(http("getInformation")
.post("/Information/$getInformation")
.body(ElFileBody("bodies/getInformtion.json"))
// I can save total, så I know the total number of entries in the entry list
.check(jsonPath("$.parameter[0].total").saveAs("total"))
//Store entire entry list
.check(jsonPath("$.parameter[0].entry").saveAs("entryList"))
//Or store all entries separatly and check afterwards who have resourceType = "dispense"? Not sure how to do this..
.check(jsonPath("$.parameter[0].entry[0]").saveAs("entry_0"))
.check(jsonPath("$.parameter[0].entry[1]").saveAs("entry_1"))
//...
.check(jsonPath("$.parameter[0].entry[X]").saveAs("entry_X"))
)
//Alternativ 1
.repeat("${total}", "counter") {
exec(session => {
//Do some magic here
//Check if session("parameter[0]_entry[counter].resourceType") = "Dispense" {
// if yes, remove entry from entry list}
session})}
//Alternativ 2
val entryString = new StringBuilder("")
.repeat("${total}", "counter") {
exec(session => {
//Do some magic here
//Check if session("parameter[0]_entry[counter].resourceType") != "Dispense" {
// if yes, add to StringBuilder}
// entryString.append(session("parameter[0]_entry[counter]").as[String] + ", ")
session})}
.exec(http("sendInformation")
.post("/Information/$sendInformation")
.body(ElFileBody("bodies/sendInformationRequest.json")))
I'm pretty new to coding
I'm using Gatling (with Scala)
Gatling with Java would probably be an easier solution for you.
check(jsonPath("$.parameter[0].entry").saveAs("entryList"))
This is going to capture a String, not a list. In order to be able to iterate, you have to use ofXXX/ofType[], see https://gatling.io/docs/gatling/reference/current/core/check/#jsonpath
Then, in order to generate the next request's body, you could consider a templating engine such as PebbleBody (https://gatling.io/docs/gatling/reference/current/http/request/#pebblestringbody) or indeed use StringBody with a function with a StringBuilder.

JSONB filter on select via Supabase

I have such a logic (attributes column's type is JSONB - array of objects) that works:
But I want to implement logical OR here if trait_type is equal ... not AND:
JSONB's column structure:
[
{
"value":"Standard Issue Armor 1 (Purple)",
"trait_type":"Clothes"
},
{
"value":"Standard Issue Helmet 1 (Red)",
"trait_type":"Full Helmet"
},
{
"value":"Chrome",
"trait_type":"SmartSkin"
},
{
"value":"Base Drone (Blue)",
"trait_type":"Drone"
},
{
"value":"Thick",
"trait_type":"Eyebrows"
}
]
How that could be done?
Thanks in advance!
I didn't verify the code, so might not work, but I believe at least is in the right direction. You can use the .or() filter to connect multiple filters with logical or operator. For contains(), you can use the cs keyword inside the or filter like this:
const { data, error } = await supabase.from('NTFs')
.select('name, id_in_collection, owner_address')
.eq('collection_id', Number(id))
.contains('attributes', JSON.stringify([{trait_type: 'SmartSkin', value: 'Chrome'}]))
.or(`attributes.cs.${JSON.stringify([{trait_type: 'Drone', value: 'Armed Drone (Blue)'}])}`, `attributes.cs.${JSON.stringify([{trait_type: 'Drone', value: 'Armed Drone (Green)'}])}`)
.order('id_in_collection')
.range(fromIndex, toIndex)

Filter 'or' and 'and' together in Loopback 4

I am using loopback 4 and trying to use filter [and] and [or] together via rest api. I have a table like this
what i want is to show only items which has cond1 = 0 and cond2 = 0 and (cond3 = 1 or cond3 = 2 or cond3 = 3).
the json would be like this
{
"and": [
{
"cond1": 0
},
{
"cond2": 0
},
{
"or": [
{
"cond3":1
},
{
"cond3":2
},
{
"cond3":3
}
]
}
]
}
this is my rest api filter
filter[where][and][0][cond1]=0&filter[where][and][1][cond2]=0&filter[where][and][2][or][0][cond3]=1&&filter[where][and][2][or][1][cond3]=2&&filter[where][and][2][or][2][cond3]=3
i've found similar post here and what i've done looks similar with Ebrahim Pasbani answers.
But instead of getting this result
I'm getting this one
which means the [and][2][or][0] syntax is miss understood by loopback as [neq]. I've tried to ask around but looks like anyone else had same problem with me and end up unsolved. i'd appreciate any answer and it possible with some explanation.
thanks before
found the answer on loopback telegram groups.
aparently you can write json string at rest api (with symbol translation for sure). so the answer is :
filter={%22%where%22:{%22and%22:[{%22or%22:[{%22cond3%22:1}{%22cond3%22:2},{%22cond3%22:3}]}],%22cond1%22:0,%22cond2%22:0}}
note: %22 => " in url string.
and voilaa....
the result is exactly as i expected. Hope this could answer for anyone who hit the same issue.

How can I find out if a List of Maps contain a specific value for key?

I'm having trouble understanding how to check if a List of Maps contain a value by a key. Below is the structure of data I have.
[
{
id: 1,
bookTxt: Hereissomebooktext.,
bookAuth: Charles
},
{
id: 3,
bookTxt: Hereissomemorebooktext.,
bookAuth: Roger
},
{
id: 6,
bookTxt: Hereissomeevenmorebooktext.,
bookAuth: Matt
}
]
I'm trying to write something simple or a function to see if this List of Maps contains a certain 'id'. I know that List has the Contains method but in my case I have to find a value within a list of Maps.
For example if I want to see if the List of Maps above contains the id of 3, how would I be able to access that?
Direct way to check
if (list[0].containsKey("id")) {
if (list[0]["id"] == 3) {
// your list of map contains key "id" which has value 3
}
}
And for indirect way you need to iterate through the loop like this:
for (var map in list) {
if (map?.containsKey("id") ?? false) {
if (map!["id"] == 3) {
// your list of map contains key "id" which has value 3
}
}
}
Maybe iterating over the maps in the list, then ask to every map to get key you want, and check the content for what you want.

RxJS filter a list based on a list

If I have a list where the number of arguments vary (or can be updated):
SomeDynamicListofPreferences = [
'Chocolate',
'Strawberry',
'Vanilla'
];
What (if any) would be a good RxJS way to dynamically filter a list of matches based on a main source stream?
StockedFlavors = [
'Almond Mocha',
'Banana',
'Butter Pecan',
'Chocolate',
'Coconut Cream',
...
'Strawberry',
'Vanilla'
];
Ex: Based on what I've learned so far one possibility is:
let dynamicPreferences = Rx.Observable.fromArray(SomeDynamicListofPreferences);
let StockedStream = Rx.Observable.fromArray(StockedFlavors);
let PrefMatches = StockedStream.filter((flavor) =>
dynamicPreferences.includes(flavor) ).
subscribe((x) => { if(x){ /*...do stuff */ }});
The "includes" operator feels like a loop within a loop. I could see another scenario of iterating the raw SomeDynamicListofPreferences to generate individual filterStreams.
Any help regarding technique and/or scale is greatly appreciated.
this works for me... ymmv
function deferSource(source) {
// wrapping in the defer gives us a unique snowflake observable
return Rx.Observable.defer(function() {
// your returned observable may vary as well...
return Rx.Observable.fromArray(source);
})
}
// your observable sources may vary
var preferenceList = [
'Chocolate',
'Strawberry',
'Vanilla'
];
var flavorList = [
'Almond Mocha',
'Banana',
'Butter Pecan',
'Chocolate',
'Coconut Cream',
'Strawberry',
'Vanilla Bean'
];
var prefrence$ = deferSource(preferenceList)
var flavor$ = deferSource(flavorList);
function curryQuery(static$source) {
return function querySourceForResults(dynamic_element) {
return static$source
.filter(function(static_element){
return static_element.includes(dynamic_element)
})
}
}
prefrence$.flatMap(curryQuery(flavor$)).subscribe(logger('result'))