Mulesoft: Group data based on date field - transformation

Can someone help me in writing the dataweave in mule 4 for
CSV input
name, dateOfSelected, joiningDate, leavingDate, age
John, 2020-02-20, 2020-02-22, 2020-04-30, 28
Sam, 2020-02-21, 2020-02-22, 2020-04-20, 30
Ben, 2020-03-03, 2020-03-04, 2020-04-29, 34
Mike, 2020-04-21, 2020-04-21, 2020-06-19, 25
expected JSON output should contain name, joiningDate and leavingDate grouped under dateOfSelected field value
{
"failedToQuery": "No",
"result": "success",
"dataAvailable" : "yes"
"2020-02-20": {
"joiningDate": "2020-02-22",
"leavingDate": "2020-04-30",
"name": "John"
},
"2020-02-21": {
"joiningDate": "2020-02-22",
"leavingDate": "2020-04-20",
"name": "Sam"
},
"2020-03-03": {
"joiningDate": "2020-03-04",
"leavingDate": "2020-04-29",
"name": "Ben"
},
"2020-04-21": {
"joiningDate": "2020-04-21",
"leavingDate": "2020-06-19",
"name": "Mike"
}
}
I will be running a db query to fetch the CSV input data using dateOfSelected field value.
In case no data returned from DB the output should look be
{
"failedToQuery": "No",
"result": "Success",
"dataAvailable" : "no"
}

You can use this script:
%dw 2.0
output application/json
---
{failedToQuery: "No"} ++
{result:"success"} ++
{dataAvailable: if (isEmpty(payload)) "no" else "yes"} ++
(payload
map $ - "age" // remove age attribute
groupBy (item, index) -> item.dateOfSelected
)

Related

graphQL filter array containing ALL

I am quite new to graphQL, and after searching the whole afternoon, i didn't found my answer to a relative quite simple problem.
I have two objects in my strapi backend :
"travels": [
{
"id": "1",
"title": "Bolivia: La Paz y Salar de Uyuni",
"travel_types": [
{
"name": "Culturales"
},
{
"name": "Aventura"
},
{
"name": "Ecoturismo"
}
]
},
{
"id": "2",
"title": "Europa clásica 2020",
"travel_types": [
{
"name": "Clasicas"
},
{
"name": "Culturales"
}
]
}
]
I am trying to get a filter where I search for travels containing ALL the user-selected travel_types.
I then wrote a query like that :
query($where: JSON){
travels (where:$where) {
id # Or _id if you are using MongoDB
title
travel_types {name}
}
And the parameter i try to input for testing :
{
"where":{
"travel_types.name_contains": ["Aventura"],
"travel_types.name_contains": ["Clasicas"]
}
}
This should return an empty array, because none of the travels have both Aventura and Clasicas travel-types.
But instead it returns the travel with id=2. It seems that only the second filter is taken.
I searched for a query which would be like Array.every() in javascript, but i wasn't able to find.
Does someone has an idea how to achieve this type of filtering ?
Thank you very much,

ReferenceManyFields (One to Many Relationship)

I am working on a project where I have to create one to many relationships which will get all the list of records referenced by id in another table and I have to display all the selected data in the multi-select field (selectArrayInput). Please help me out in this, if you help with an example that would be great.
Thanks in advance.
Example:
district
id name
1 A
2 B
3 C
block
id district_id name
1 1 ABC
2 1 XYZ
3 2 DEF
I am using https://github.com/Steams/ra-data-hasura-graphql hasura-graphql dataprovider for my application.
You're likely looking for "nested object queries" (see: https://hasura.io/docs/1.0/graphql/manual/queries/nested-object-queries.html#nested-object-queries)
An example...
query MyQuery {
district(where: {id: {_eq: 1}}) {
id
name
blocks {
id
name
}
}
}
result:
{
"data": {
"district": [
{
"id": 1,
"name": "A",
"blocks": [
{
"id": 1,
"name": "ABC"
},
{
"id": 2,
"name": "XYZ"
}
]
}
]
}
}
Or...
query MyQuery2 {
block(where: {district: {name: {_eq: "A"}}}) {
id
name
district {
id
name
}
}
}
result:
{
"data": {
"block": [
{
"id": 1,
"name": "ABC",
"district": {
"id": 1,
"name": "A"
}
},
{
"id": 2,
"name": "XYZ",
"district": {
"id": 1,
"name": "A"
}
}
]
}
}
Setting up the tables this way...
blocks:
districts:
Aside: I recommend using plural table names as they are more standard, "districts" and "blocks"

Is there any way to exclude the "meta", "rows", "statistics" fields from the ClickHouse FORMAT JSON query?

Query:
SELECT value FROM test.json_test FORMAT JSON
Response:
{
"meta":
[
{
"name": "value",
"type": "Int32"
}
],
"data":
[
{
"value": 1
}
],
"rows": 1,
"statistics":
{
"elapsed": 0.112135109,
"rows_read": 1,
"bytes_read": 4
}
}
How to exclude unnecessary fields and leave only data field?
You can exclude "statistics" from output:
set output_format_write_statistics=0;
ubuntu-16gb-nbg1-1 :) select 1 format JSON;
SELECT 1
FORMAT JSON
{
"meta":
[
{
"name": "1",
"type": "UInt8"
}
],
"data":
[
{
"1": 1
}
],
"rows": 1
}
1 rows in set. Elapsed: 0.003 sec.
It looks like JSONEachRow cannot be used straightforward because the result JSON is not valid.
You can play with grouping data to the array to get valid JSON:
SELECT
groupArray(value) AS values,
groupArray((value, name)) AS objects
FROM
(
SELECT
1 AS value,
'str1' AS name
UNION ALL
SELECT
2 AS value,
'str2' AS name
)
FORMAT JSONEachRow
/* Result:
{"values":[1,2],"objects":[[1,"str1"],[2,"str2"]]}
*/
where you try running your query?
maybe you can try filter this output via pipelines and JQ
clickhouse-client -q "SELECT value FROM test.json_test FORMAT JSON" | jq .data

unable to parse json into csv using jq

I have a JSON file that I want to convert into a CSV file using the jq in a shell script. I want to create a single row from this entire JSON file. I have to extract value from values. The row output should be something like
null,642,642,412,0,null,null
Here is my JSON file
{
"data": [
{
"name": "exits",
"period": "lifetime",
"values": [
{
"value": {}
}
],
"title": "Exits",
"description": "Number of times someone exited the carousel"
},
{
"name": "impressions",
"period": "lifetime",
"values": [
{
"value": 642
}
],
"title": "Impressions",
"description": "Total number of times the media object has been seen"
},
{
"name": "reach",
"period": "lifetime",
"values": [
{
"value": 412
}
],
"title": "Reach",
"description": "Total number of unique accounts that have seen the media object"
},
{
"name": "replies",
"period": "lifetime",
"values": [
{
"value": 0
}
],
"title": "Replies",
"description": "Total number of replies to the carousel"
},
{
"name": "taps_forward",
"period": "lifetime",
"values": [
{
"value": {}
}
],
"title": "Taps Forward",
"description": "Total number of taps to see this story's next photo or video"
},
{
"name": "taps_back",
"period": "lifetime",
"values": [
{
"value": {}
}
],
"title": "Taps Back",
"description": "Total number of taps to see this story's previous photo or video"
}
]
}
Hi tried using this jq command :
.data | map(.values[].value) | #csv
This is giving the following output:
jq: error (at :70): object ({}) is not valid in a csv row
exit status 5
So when I am getting this empty JSON object it is reflecting an error.
Please Help!!
The row output should be something like
null,642,642,412,0,null,null
Using length==0 here is dubious at best. To check for {} one could write:
jq '.data | map(.values[].value | if . == {} then "null" else . end) | #csv'
Similarly for [].
If you run the command without the #csv part you will see that the output is:
[
{},
642,
412,
0,
{},
{}
]
By replacing the empty objects with "null": (length == 0)
jq '.data | map(.values[].value) | map(if (type == "object" and length == 0 ) then "null" else . end) | #csv'
Output:
"\"null\",642,412,0,\"null\",\"null\""
Per suggestion from #aaron (see comment). The following can produce the requested output without extra post-processing. Disclaimer: this is not working with my jq 1.5, but working on jqplay with jq 1.6.
jq --raw-output '.data | map(.values[].value) | map(if (type == "object" and length == 0 ) then "null" else . end) | join(",")'
Output:
null,642,412,0,null,null

Applying dataweave filter option or conditional check in nested lists - Mule

I have an object of a similar structure. I need to filter the ProductInfo objects on start date and end date, and it can be selected even if any one of the revision dates fall within the interval. I have the user input , start date and end date as a hash
{
productInfo : [
{
Id: XXX,
Revisions:[
{
startDate : yyyy
endDate : zzz
}
{
startDate : yyyy
endDate : zzz
}
]
and more productInfo objects.....
]}
map in a flow variable.
I cannot share my configuration. Can anyone help me with the dataWeave syntax for the above scenario.
Thanks
For this purpose we can declare Functions in the Header and refer it from DataWeave body. Here, I am using following JSON input:
{
"productInfo": [
{
"Id": "1",
"Revisions": [
{
"startDate": "20160101",
"endDate": "20160131"
},
{
"startDate": "20160201",
"endDate": "20160229"
}
]
},
{
"Id": "2",
"Revisions": [
{
"startDate": "20160301",
"endDate": "20160330"
},
{
"startDate": "20160401",
"endDate": "20160430"
}
]
}
]
}
And test it using this script:
%dw 1.0
%output application/json
%var filterStartDate = "20160101" //flowVars.inputDate["startDate"]
%var filterEndDate = "20160130" //flowVars.inputDate["endDate"]
%function evaluateRevisions(revisions) (
(revisions map {
evaluated: ($.startDate <= filterStartDate) and ($.endDate >= filterEndDate)
})
)
---
payload.productInfo map using (productInfo = $) {
filteredProduct: productInfo,
found: evaluateRevisions($.Revisions).evaluated contains true
} filter $.found
I can get the productInfo based on input date (startDate and endDate):
[
{
"filteredProduct": {
"Id": "1",
"Revisions": [
{
"startDate": "20160101",
"endDate": "20160131"
},
{
"startDate": "20160201",
"endDate": "20160229"
}
]
},
"found": true
}
]

Resources