How to represent this response using Dataweave? - anypoint-studio

I have a HTTP requester in my flow which calls AmericanFlightsAPI, its original GET response is like this:
[{
"ID": 1,
"code": "ER38sd",
"price": 400.00,
"departureDate": "2016/03/20",
"origin": "MUA",
"destination": "SFO",
"emptySeats": 0,
"plane": {
"type": "Boeing 737",
"totalSeats": 150
}
I want to filter these records and show only records containing flights having destination "SFO" and "CLE", like this:
{
"flight_ID": 4,
"code": "rree1000",
"destination": {
"destination": "CLE"
}
Other fields for the filtered destination like ID, Code should also be removed from the response.
What is the Dataweave code to achieve this? Actual API is like this: Original API

Quick and easy way could be ..
%dw 2.0
output application/json
---
payload filter ((value, index) -> (value.destination == 'SFO' or value.destination == 'CLE')) map {
"flight_ID" : $.ID,
"code": $.code,
"destination": {
"destination": $.destination
}
}

Related

Get the value from the response based on a condition and store it to a variable

I would like to get the value from the response based on a condition and store it to a variable.
In the below JSON, I would like to store the value when the name matches to something I prefer. Is there a way to achieve this using Karate API?
{
"results": [
{
"name": "Sample1",
"email": "sample1#text.com",
"id": "U-123"
},
{
"name": "Sample2",
"email": "sample2#text.com",
"id": "U-456"
},
{
"name": "Sample3",
"email": "sample3#text.com",
"id": "U-789"
}
]
}
So after reading the comment, my interpretation is to "find" the id where name is Sample2. Easy, just use a filter() operation, refer the docs: https://github.com/karatelabs/karate#jsonpath-filters
Instead of using a filter, I'm using the JS Array find() method as a very concise example below:
* def response =
"""
{ "results": [
{ "name": "Sample1", "email": "sample1#text.com", "id": "U-123" },
{ "name": "Sample2", "email": "sample2#text.com", "id": "U-456" },
{ "name": "Sample3", "email": "sample3#text.com", "id": "U-789" }
]
}
"""
* def id = response.results.find(x => x.name == 'Sample2').id
* match id == 'U-456'
Take some time to understand how it works. Talk to someone who knows JS if needed.

Jmeter extracting response using JSON Path

Hi Can someone help me simulate this scenario, Example this is the response I got, I want to extract all alertId with the name parameter contains test. You response is highly appreciated. Thank you so much.
Response:
[
{
"duplicateCount": 0,
"fqdn": "qa-ubuntu14-4",
"appName": "TEST_APD_UB14",
"stateString": "OPEN",
"category": "FILESCAN",
"alkey": {
"agentId": "8470ea64-a710-3e46-ba6b-ccd37ebc4074",
"role": "AD SERVER",
"alertId": "0258a7ca-bc72-3a53-aa98-3098c87411ba",
"id": "6695a7fa-ab9f-43fa-871b-620cd1eeb75054af7770-604b-11e9-b486-8d59ab9344597cea0ea2-d897-3696-852d-5f3cb36f270e8470ea64-a710-3e46-ba6b-ccd37ebc4074/var/log/test321.txttest321.txtA",
"applicationContextId": "7cea0ea2-d897-3696-852d-5f3cb36f270e"
},
"properties": {
"name": "test321.txt",
"acl": ""
}
},
{
"duplicateCount": 0,
"fqdn": "qa-ubuntu14-4",
"appName": "TEST_APD_UB18",
"stateString": "OPEN",
"category": "FILESCAN",
"alkey": {
"agentId": "8470ea64-a710-3e46-ba6b-ccd37ebc4074",
"role": "AD SERVER",
"alertId": "0258a7ca-bc72-3a53-aa98-3098c8741CDA",
"id": "6695a7fa-ab9f-43fa-871b-620cd1eeb75054af7770-604b-11e9-b486-8d59ab9344597cea0ea2-d897-3696-852d-5f3cb36f270e8470ea64-a710-3e46-ba6b-ccd37ebc4074/var/log/test321.txttest321.txtA",
"applicationContextId": "7cea0ea2-d897-3696-852d-5f3cb36f270e"
},
"properties": {
"name": "test555.txt",
"acl": ""
}
}
]
Screenshot:
Expected Result:
I want to extract all alertId with the name parameter contains test
You could use the following JSON query to extract the values:
[*].[?(#.properties.name contains 'test')]alkey.agentId
I found this reference with JSON Path Syntax is really useful.

mule filter logic processing in dataweave

I have a Json payload i.e.
{
"Numbers": [
0
],
"title": "string",
"Ids": [
0, 1, 2
],
"group": 0
}
and I am hitting a http call to get collection of records for all customers present in DB i.e.
{
"details": [{
"detailId": 0,
"Id": 0,
"name": "string",
"place": "string",
"country": "string",
"bloodgroup": "string",
"area": "string"
},
{
"detailId": 0,
"Id": 1,
"name": "string",
"place": "string",
"country": "string",
"bloodgroup": "string",
"area": "string"
}
]
}
Now I want to filter and extract the data for those Ids for which I am getting in Request payload in Ids field and match them in collection response payload.
ID field is common in Request payload and in Response payload
Please help me on this.
Let's assume you're storing the first payload in a variable called flowVars.ids. You want to use the contains method, which works returns true if the value you're checking is contained in the array, otherwise it returns false. Here's a couple examples:
// Returns true
[1,2,3,4,5] contains 5
// Returns false
[1,2,3,4,5] contains "Hello"
So you could structure your filter like this (assuming Mule 3.x and DataWeave 1.0):
%dw 1.0
%output application/java
%var ids = flowVars.ids.Ids
---
payload.details filter (ids contains $.Id)

How to create a json message from 2 different json message using Dataweave

I want to create a json message from 2 different Json message. Both input messages are coming from flow variables. How can it be done using Dataweave.
Message 1:
[
{
"userId": 11,
"name": "ABC",
"age": 30
},
{
"userId": 44,
"name": "XYZ",
"age": 30
}
]
Message 2:
[
{
"userId": 11,
"Address": "BLR"
},
{
"userId": 44,
"Address": "CCU"
}
]
Expected output:
[
{
"userId": 11,
"name": "ABC",
"Address": "BLR"
},
{
"userId": 44,
"name": "XYZ",
"Address": "CCU"
}
]
Thank You in advance,
Nitesh
Refer this article. Seems the perfect solution for your usecase.
You can use lookup instead of filter as using filter is performance overhead.
Try this
%dw 1.0
%output application/json
%var adressLookup = {( flowVars.data map {
($.userId ++ "") : $
})}
---
payload map {
userId : $.userId,
name : $.name,
Address : adressLookup[$.userId ++ ""].Address
}
where payload is message1 and flowVars.data is message2
P.S : I have used $.userId ++ "" as key in string format for hashmap, some how numeric key isn't work with weave hashmap.
also refer Merge two json payload with dataweave
Hope this helps.

Is it possible to extend graphql response other than just data for pagination?

In GraphQL response normally looks like followings.
{
"data": [{
"id": 1,
"username": "Jon Snow",
"email": "crow#northofthew.all",
"age": 20
}, {
"id": 2,
"username": "Tyrion Lannister",
"email": "drunk#i.mp",
"age": 34
}, {
"id": 3,
"username": "Sansa Stark",
"email": "redhead#why.me",
"age": 17
}]
}
Is it possible to add meta data to your response such as pagination like this.
{
"pagination": {
"total": 14,
"count": 230,
},
"data": [{
"id": 1,
"username": "Jon Snow",
"email": "crow#northofthew.all",
"age": 20
}, {
"id": 2,
"username": "Tyrion Lannister",
"email": "drunk#i.mp",
"age": 34
}]
}
I'm using express-graphql and currently put those pagination to custom response header, which is fine but it can be better. Since GraphQL response is already wrapped with "data", it is not very strange to add more "data" to its response.
Reenforcing what #CommonsWare already stated, according to the specification that would a be an invalid GraphQL response. Regarding pagination, Relay has its own pagination approach called connections, but indeed, several other approaches are possible and even more suitable in some situtations (connections aren't a silver bullet).
I want to augment what was already said by adding that the hierarchical nature of GraphQL incites related data to be at the same level. An example is worth a thousands words, so here it goes:
query Q {
pagination_info { # what is this info related to? completely unclear
total
count
}
user {
friends {
id
}
}
}
Instead...
query Q {
user {
friends {
pagination_info { # fairly obvious that this is related to friends
total
count
}
friend {
id
}
}
}
}