Dataweave 2 transforming an array of objects having a id field, into a object whose elements are maps - transformation

need some suggestion for a dataweave 2 transformation that can transform the following input (Array of Object having an id field) to the following output (grouping in an array the objects sharing the same id into a map, having the id as key).
Input :
[
{
"id": "1117",
"Tot": "10.0",
"Per": "7/2025"
},
{
"id": "1117",
"Tot": "200.0",
"Per": "2/2021"
},
{
"id": "7997",
"Tot": "78.0",
"Per": "10/2023"
}
]
output
{
"1117": [
{
"id": "1117",
"Tot": "10.0",
"Per": "7/2025"
},
{
"id": "1117",
"Tot": "200.0",
"Per": "2/2021"
}
],
"7997": [
{
"id": "7997",
"Tot": "78.0",
"Per": "10/2023"
}
]
}
Any idea?
Thanks

%dw 2.0
output application/json
---
payload groupBy $.id
You just want to group by the ID? There ya go.
I would recommend going to https://developer.mulesoft.com/learn/dataweave and then going to the tutorial tab (top right). It will walk through these basic scenarios :)

payload groupBy $.id
Adding more chars to reach 30.

Related

Directus Filtering multiple fields

I'm trying to filter Directus CMS data set through URL parameters.
This is a sample data set. I can successfully filter data set by single parameter.
{
"data":[
{
"id": "1",
"status": "published",
"category": "Novel",
"section": "Kids"
},
{
"id": "2",
"status": "published",
"category": "Novel",
"section": "Adults"
}
]
}
/items/books?filter[category][_eq]=Novel
gives me exactly what I expected which is 1 & 2 data records.
But I need to filter both "category" & "section" fields
/items/books?filter[category][_eq]=Novel&filter[section][_eq]=Adults
For above I receive an empty data set.
Why is this getting failed ? Where do I need to fix? Appreciate your support in advance. Thanks!
Try the following query:
/items/books?filter={"_or":[{"category":{"_eq": "Novel"}},{"section":{"_eq":"Adults"}}]}
An expanded version of the filter:
"_or": [
{
"category": {
"_eq": "Novel"
}
},
{
"section": {
"_eq": "Adults"
}
}
]
Visit the official docs to read more about filtering rules and logical operators.

Incorrectly selected data in the query

Only articles that contain the EmailMarketing tag are needed.
I'm probably doing the wrong search on the tag, since it's an array of values, not a single object, but I don't know how to do it right, I'm just learning graphql. Any help would be appreciated
query:
query {
enArticles {
title
previewText
tags(where: {name: "EmailMarketing"}){
name
}
}
}
result:
{
"data": {
"enArticles": [
{
"title": "title1",
"previewText": "previewText1",
"tags": [
{
"name": "EmailMarketing"
},
{
"name": "Personalization"
},
{
"name": "Advertising_campaign"
}
]
},
{
"title": "title2",
"previewText": "previewText2",
"tags": [
{
"name": "Marketing_strategy"
},
{
"name": "Marketing"
},
{
"name": "Marketing_campaign"
}
]
},
{
"title": "article 12",
"previewText": "article12",
"tags": []
}
]
}
}
I believe you first need to have coded an equality operator within your GraphQL schema. There's a good explanation of that here.
Once you add an equality operator - say, for example _eq - you can use it something like this:
query {
enArticles {
title
previewText
tags(where: {name: {_eq: "EmailMarketing"}}){
name
}
}
}
Specifically, you would need to create a filter and resolver.
The example here may help.

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,

Receive JSON data from Neo4j to Spring Boot API

I'm trying to send data from Neo4J to Spring boot and I want to receive a JSON like this:
{
"name": "Alex Statham",
"people": [
{
"name": "Jason Statham",
"people": [
{
"name": "Lyna Statham"
},
{
"name": "John Statham"
}
]
},
{
"name": "Will Statham",
"people": [
{
"name": "Michael Statham"
}
]
}
]
}
I try many query and this one maybe the best query which returns a correct family tree for all cases, but it only return nodes with no relationship so I can't put that data to the Library I use:
MATCH (p1:Person {maBN:3})
CALL apoc.path.subgraphNodes(p1, {
sequence: '>Person,FCHILD,>Person,FCHILD,>Person',
maxLevel: 6
}) YIELD node
RETURN node, p1
Here is that Library: https://www.npmjs.com/package/react-tree-graph
What should I do? I don't know too much about Cypher and Neo4j.
You could use apoc.convert.toTree which takes a list of paths and creates a structure like yours.

How to convert this complex JSON into spring boot and store in sql server

[
{
"Name": "xyz",
"age": "20",
"socialMedia": [
{
"fb": "aaa"
},
{
"insta": "bbb"
}
],
"languge": "eng",
"country": "USA",
"Experiance": [
{
"developer": {
"companyName": "abcd",
"years": "5"
},
"tester": {
"companyName": "efgh",
"years": "3"
}
}
]
}
]
i am getting this json from front-end and i need to convert this json into spring-boot code and store in H2(in-memory) database......
intentionally i am not sharing my code.....
your answer might help me to enrich my code ...
so, please try to give your best using advanced concepts in spring-boot
Thanks In Advance
Here, you can look about JSON data types: JSON data types
The link includes that how data types present JSON.
Then you can configure object model for JSON data.

Resources