Mulesoft - Build JSON List dynamically - mule

I am a Mulesoft newbie trying to dynamically add data to a JSON list according to the payload but currently I am stuck on it.
Below is an example of what I pretend to return:
{
"id": "1",
"name": "Example A",
"languages": [
{
"code": "en",
"label": "English",
"translatedName": "Example A"
}
]
},
{
"id": "2",
"name": "Example B",
"languages": [
{
"languageCode": "nl",
"translatedName": "Voorbeeld B"
},
{
"languageCode": "fr",
"translatedName": "Exemple B"
}
]
}
As you can see, the languages depend on the name (Example A has 1 language, Example B has 2).
Am I able to build a map, or a function, in order to return the correct languages?
Best regards and thank you in advance!

Related

How to use loop for response from another feature?

Іssue in the next.
I have a feature response which I check according to schema validation
{
"name": "#string",
"director_first_name": "##string",
"director_last_name": "##string",
"director_phone": "##string",
"director_email": "##string",
"language": {
"id": "#uuid",
"name": "#string",
"code": "#string? _.length == 2"
}
}
Also I have additional feature, which has list of languages
[
{
"id": "fde1312f-2ab2-4fdf-a4f3-a7095dd89a4d",
"name": "English",
"code": "EN"
},
{
"id": "0d4c6626-1010-4dda-8721-665071ec3b28",
"name": "Swedish",
"code": "SV"
}
]
And I need to check the next
Need to take response.language.id from first response and check if this id is represented in the second response. In this case I need to call this second feature.
If it is represented, need to match if id, name, code which belong to first reponse the same as in the second response.
You can do this in one line. I leave it as an exercise to you to get the data from a second feature file if you wish.
* def data =
"""
[
{
"id": "fde1312f-2ab2-4fdf-a4f3-a7095dd89a4d",
"name": "English",
"code": "EN"
},
{
"id": "0d4c6626-1010-4dda-8721-665071ec3b28",
"name": "Swedish",
"code": "SV"
}
]
"""
* def response =
"""
{
"language": {
"id": "fde1312f-2ab2-4fdf-a4f3-a7095dd89a4d",
"name": "English",
"code": "EN"
}
}
"""
* match response.language == data.find(x => x.code == response.language.code)
Take some time to read other answers (and follow the links) for ideas: https://stackoverflow.com/a/70055035/143475

Microsoft adaptive card choiceset iteration

i have a adaptive card choice set like below, as you can see am trying to get the value under title from a variable which is an array, is there a way i can iterate the choice set automatically because i don't know how many values the array has i want to show all the values inside the array in the choice set title
{
"type" : "Input.ChoiceSet",
"isMultiSelect": true,
"id": "myColor",
"style": "compact",
"value": "1",
"choices": [
{
"title": vars.responsedata.items[0].topic,
"value": "1"
},
{
"title": vars.responsedata.items[1].topic,
"value": "2"
},
{
"title": "Recording 3 sample",
"value": "3"
}
]
}
You can use the map() function.
Example in DataWeave:
{
choices: vars.responsedata.items map {
title: $.topic,
value: $$
}
}

How to match field value in response when there are multiple fields with the same name?

[
{
"key": "test1",
"category": "test",
"name": "test1",
"translations":
{
"english": "eng"
}
},
{
"key": "test2",
"category": "test",
"name": "test1",
"translations":
{
"english": "eng2",
"german": "German"
}
},
{
"key": "test3",
"category": "power",
"name": "test1",
"translations":
{
"EN_lang": "jik"
}
}
]
Here, we have multiple field's are with different values and we have to match value in translations (field position will change on every call)
You have to be clear about what you want to assert. Hint, the new contains deep (available in 0.9.6.RC4) can help:
* match response contains deep { key: 'test2', translations: { english: 'eng2' } }
Else you should look at transforming the JSON into a shape where it is easier to do the assertions you want: https://github.com/intuit/karate#json-transforms

Apache Nifi: UpdateRecord replace child values

I'm trying to use UpdateRecord 1.9.0 processor to modify a JSON but it does not replace the values as I want.
this is the source message
{
"type": "A",
"ids": [{
"id": "1",
"value": "abc"
}, {
"id": "2",
"value": "def"
}, {
"id": "3",
"value": "ghi"
}
]
}
and the wanted output
{
"ids": [{
"userId": "1",
}, {
"userId": "2",
}, {
"userId": "3",
}
]
}
I have configured the processor as follows
processor config
Reader:
reader
Schema registry:
schema
writer:
writer
And it works, the output is a JSON without the field 'type' and the ids have the field 'userId' instead 'id' and 'value'.
To fill the value of userId, I defined the replace strategy and the property to replace:
strategy
But the output is wrong. The userId is always filled with the id of the last element in the array:
{
"ids": [{
"userId": "3"
}, {
"userId": "3"
}, {
"userId": "3"
}
]
}
I think the value of the expression is ok because if I try to replace only one record it works fine (/ids[0]/userId, ..id)
Nifi docs has a really similar example (example 3):
https://nifi.apache.org/docs/nifi-docs/components/org.apache.nifi/nifi-standard-nar/1.7.1/org.apache.nifi.processors.standard.UpdateRecord/additionalDetails.html
But it does not work for me.
What am I doing wrong?
thanks
Finally I have used JoltJSONTransform processor instead UpdateRecord
JoltJSONTransform
template:
[
{
"operation": "shift",
"spec": {
"ids":{
"*":{
"id": "ids[&1].userId"
}
}
}
}
]
Easier than UpdateRecord

Specific analyzers for sub-documents in lucene / elasticsearch

After reading the documentation, testing and reading a lot of other questions here on stackoverflow:
We have documents that have titles and description in multiple languages. There are also tags that are translated to the same languages. There might be up to 30-40 different languages in the system, but probably only 3 or 4 translations for a single document.
This is the planned document structure:
{
"luck": {
"id": 10018,
"pub": 0,
"pr": 100002,
"loc": {
"lat": 42.7,
"lon": 84.2
},
"t": [
{
"lang": "en-analyzer",
"title": "Forest",
"desc": "A lot of trees.",
"tags": [
"Wood",
"Nature",
"Green Mouvement"
]
},
{
"lang": "fr-analyzer",
"title": "Forêt",
"desc": "A grand nombre d'arbre.",
"tags": [
"Bois",
"Nature",
"Mouvement Vert"
]
}
],
"dates": [
"2014-01-01T20:00",
"2014-06-06T20:00",
"2014-08-08T20:00"
]
}
}
Possible queries are "arbre" or "wood" or "forest" or "nature" combined with a date and a geo_distance filter, furthermore there will be some facets over the tags array (that obviously include counting).
We can produce any document structure that fits best for elasticsearch (or for lucene). It's crucial that each language is analyzed specifically, so we use "_analyzer" in order to distinguish the languages.
{
"luck": {
"properties": {
"id": {
"type": "long"
},
"pub": {
"type": "long"
},
"pr": {
"type": "long"
},
"loc": {
"type": "geo_point"
},
"t": {
"_analyzer": {
"path": "t.lang"
},
"properties": {
"lang": {
"type": "string"
},
"properties": {
"title": {
"type": "string"
},
"desc": {
"type": "string"
},
"tags": {
"type": "string"
}
}
}
}
}
}
A) Apparently, this idea does not work: after PUTting the mapping, we retrieve the same mapping ("GET") and it seems to ignore the specific analyzers (A test with a top-level "_analyzer" worked fine). Does "_analyzer" work for sub-documents and if yes how to should we refer to it? We also tested declaring the sub-document as "object" or "nested". How is multi-language document indexing supposed to work.
B) One possibility would be to put each language in its own document: In that case how do we manage the id? Finally both documents should refer to the same id. For example if the user searches for "nature" (and we don't know if the user intends to find "nature" in English or French), this document would appear twice in the result set, and the counting and paging would be very wrong (also facet counting).
Any ideas?