How to map an array within an array and achieve the following output? - mule

In the given input,
{
"editable": true,
"sections": [
{
"title": "Identification",
"calingaKey": "",
"content": [
[{
"name": "Classification",
"text": "Product",
"url": "",
"info": ""
},
{
"name": "Product Number",
"text": "####1234",
"url": "",
"info": ""
}]
]
},
{
"title": "Position and Contact",
"calingaKey": "",
"content": [
[{
"name": "Manufacturer",
"text": "Value of Manufacturer",
"url": "",
"info": ""
},
{
"name": "Hardware Version",
"text": "####1234",
"url": "",
"info": ""
}]
]
}
]
}
"content" is an array of array of objects. Basically, the "name" field has to be replaced by values stored in their corresponding keys in the "calinga" variable.
I could do it for the "title" field, but each "name" field should also be replaced by it's name in the variable.
%dw 2.0
output application/json
var calinga = {
"Identification": "Identifikation",
"Position and Contact": "Positions und Contacts",
"Classification": "Classifikation",
"Product Number": "Produkt Number",
"Manufacturer": "Manufakturer",
"Hardware Version": "Hware Vsion"
}
---
{
"editable": payload.editable,
"sections": payload.sections map(item01, index01)->{
"title": calinga[item01.title],
"content": item01.content map(item02)->(item02)
}
}
How Can I achieve the following output?
{
"editable": true,
"sections": [
{
"title": "Identifikation",
"calingaKey": "",
"content": [
[{
"name": "Classifikation",
"text": "Product",
"url": "",
"info": ""
},
{
"name": "Produkt Number",
"text": "####1234",
"url": "",
"info": ""
}]
]
},
{
"title": "Positions und Contacts",
"calingaKey": "",
"content": [
[{
"name": "Manufakturer",
"text": "Value of Manufacturer",
"url": "",
"info": ""
},
{
"name": "Hware Vsion",
"text": "####1234",
"url": "",
"info": ""
}]
]
}
]
}

You can use mapObject() once you descend from the last nested array into objects. Then the trick is to use the value of calinga but if it null because the key is not present then use the original value as the default: item03 mapObject {($$):calinga[$] default $}.
Example:
%dw 2.0
output application/json
var calinga = {
"Identification": "Identifikation",
"Position and Contact": "Positions und Contacts",
"Classification": "Classifikation",
"Product Number": "Produkt Number",
"Manufacturer": "Manufakturer",
"Hardware Version": "Hware Vsion"
}
---
{
"editable": payload.editable,
"sections": payload.sections map(item01, index01)->{
"title": calinga[item01.title],
"content": item01.content map(item02)->(item02 map(item03)-> item03 mapObject {($$):calinga[$] default $})
}
}

Related

Dynamically trying to update a field in an object using dataweave, how to achieve the same?

The use case is : we'll be having translations stored in an object in a variable in the dataweave, we have to dynamically assign those translations in the "calingaKey" value, I tried doing something below, but it isn't working.
Input
{
"editable": true,
"sections": [
{
"title": "Identification",
"calingaKey": "",
"content": [
{
"name": "Classification",
"text": "Product",
"url": "",
"info": ""
},
{
"name": "Product Number",
"text": "####1234",
"url": "",
"info": ""
}
]
},
{
"title": "Position and Contact",
"calingaKey": "",
"content": [
{
"name": "Manufacturer",
"text": "Value of Manufacturer",
"url": "",
"info": ""
},
{
"name": "Hardware Version",
"text": "####1234",
"url": "",
"info": ""
}
]
}
]
}
Dataweave
%dw 2.0
import * from dw::util::Values
output application/json
var calinga = {
"title": "titel",
"Position and contact": "Lage und Kontakt"
}
---
payload.sections map (item,index)->(item mapObject ((value, key, index) -> {'$(key)' : value} update field("calingaKey") with calinga.'$(key)' ))
"calinga.'$(key)'" doesn't seem to work for some reason, and gives me null, any issue with my code?
Expected Output
[
{
"title": "Identification",
"calingaKey": "titel",
"content": [
{
"name": "Classification",
"text": "Product",
"url": "",
"info": ""
},
{
"name": "Product Number",
"text": "####1234",
"url": "",
"info": ""
}
]
},
{
"title": "Position and Contact",
"calingaKey": "titel",
"content": [
{
"name": "Manufacturer",
"text": "Value of Manufacturer",
"url": "",
"info": ""
},
{
"name": "Hardware Version",
"text": "####1234",
"url": "",
"info": ""
}
]
}
]
%dw 2.0
import * from dw::util::Values
output application/json
var calinga = {
"title": "titel1",
"calingaKey": "titel1",
"Position and contact": "Lage und Kontakt"
}
---
payload.sections map (item,index)->(item mapObject ((value, key, index) -> {'$(key)' : value} update field("calingaKey") with calinga[key]
))

Convert TEXT file to JSON in Dataweave

How can I convert TEXT file into JSON in Dataweave.
Input:
"[
{
"quantity": "1.0",
"uom": "every",
"amount": "5.0",
"allocation": [
{
"ID": "22245",
"price": "156",
"desc": "Product A1"
}
],
},
{
"quantity": "2.0",
"uom": "all",
"amount": "65888",
"allocation": [
{
"ID": "65665",
"price": "7789",
"desc": "Product A2"
}
],
}
]"
Expected output in JSON:
ex:
[
{
"quantity": "1.0",
"uom": "every",
"amount": "5.0",
"allocation": [
{
"ID": "22245",
"price": "156",
"desc": "Product A1"
}
],
},
{
"quantity": "2.0",
"uom": "all",
"amount": "65888",
"allocation": [
{
"ID": "65665",
"price": "7789",
"desc": "Product A2"
}
],
}
]
enter image description here
Use the read() function to parse the input string as JSON.
{
ex: read(payload,"application/JSON")
}

How to parse a JSON with missing property name into a db table

I am trying to convert JSON into an SQL table. The source is the response from a REST API. I do not have the opportunity to change the response.
The response contains data on several projects. Projects are returned as individual members of a global projects object.
As the projectid is not at the same level with the other data and also does not have a name
SELECT * FROM OPENJSON (#JSON);
fails to parse it properly. I end up with one row for projects and the json object as the value. Also without the proper way to refer to the elements I could not figure out a way to use JSON_VALUE.
Below is a sample structure. Actual data has much more properties and variations between the projects. I prefer to solve this in SQL. But any way to sort out the JSON will be appreciated. This is my first exposure to JSON. I want to ask if I am missing something very obvious...
TIA
{
"projects": {
"project1id": {
"data": {
"customer": "Cust1",
"name": "Project Name 1"
},
"projectType": "type0"
},
"project2id": {
"data": {
"customer": "Customer 2",
"name": "Name 2",
"projectManager": "Man Ager"
},
"projectType": "type2"
},
"Project3id": {
"data": {
"customer": "Another Customer",
"name": "Another Project"
},
"projectType": "type1"
}
}
}
Expected Result
ProjectId
Project Name
Customer
Project Type
Project Manager
project1id
Project Name 1
Cust 1
type0
project2id
Customer 2
type2
Man Ager
project3id
Another Project
Another Customer
type1
Excerpt from original Json:
{
"projects": {
"10000eumbvqn76": {
"data": {
"inquiryNumber": "34635",
"customer": "C AS",
"name": "E W ",
"orderNumber": "1000",
"seller": "M A",
"projectManager": "B O V",
"phase": "fulfillment",
"exchange": {
"deadline": {
"time": [
24,
0
],
"timezone": [
1,
0
]
},
"settings": {
"client": {
"codes": [
{
"id": "1",
"label": "Code 1"
},
{
"id": "4",
"label": "Code 4"
},
{
"id": "5",
"label": "Code 5"
},
{
"id": "2",
"label": "Code 2"
},
{
"id": "3",
"label": "Code 3"
}
],
"approvedCodes": [
"1"
],
"cycles": {
"producer": 21,
"consumer": 21
}
},
"clientForInformation": {
"cycles": {
"producer": 21
}
},
"supplier": {
"codes": [
{
"id": "1",
"label": "Code 1"
},
{
"id": "4",
"label": "Code 4"
},
{
"id": "5",
"label": "Code 5"
},
{
"id": "2",
"label": "Code 2"
},
{
"id": "3",
"label": "Code 3"
}
],
"approvedCodes": [
"1"
],
"cycles": {
"producer": 14,
"consumer": 14
}
},
"supplierIsProducer": {
"supplierRole": "producer"
},
"supplierIsConsumer": {
"supplierRole": "consumer"
}
},
"sequences": {
"$salesPurchase": {
"label": "Client RFQ to OC",
"settings": "supplierIsProducer",
"group": "inquiry"
},
"$salesPurchaseSupplier": {
"label": "Supplier RFQ to OC",
"settings": "supplierIsConsumer",
"group": "inquiry"
},
"$salesClient": null,
"$salesClientFrom": null,
"$client": {
"label": "To client",
"group": "order-fulfillment",
"order": [
"IFR",
"IFI"
],
"stages": {
"IFR": {
"label": "Issued for Review",
"phase": "forApproval",
"settings": "client"
},
"IFI": {
"label": "Issued for Information",
"phase": "forInformation",
"settings": "clientForInformation"
}
},
"interpret": {
"type": "unordered"
}
},
"$supplier": {
"label": "From supplier",
"group": "order-fulfillment-supplier",
"settings": "supplier"
},
"$supplierTo": {
"label": "To supplier",
"group": "order-fulfillment-supplier",
"settings": "supplierIsConsumer"
},
"$internal": null
}
},
"officialMailIdFormat": "M-1000-0001",
"transmittalMailIdFormat": "TR-1000-0001",
"commercialMailIdFormat": "Bid-34635-0001",
"officialMailIdFormats": [
{
"label": "Official",
"format": "M-1000-0001"
},
{
"label": "Commercial",
"format": "Bid-34635-0001"
}
]
},
"projectType": "commercial"
},
"1000hf30ua": {
"data": {
"inquiryNumber": "100",
"customer": "S M I Y P L",
"name": "1000 FSPO ",
"seller": "L H",
"projectManager": "L H",
"phase": "inquiry",
"exchange": {
"deadline": {
"time": [
24,
0
],
"timezone": [
1,
0
]
},
"settings": {
"client": {
"codes": [
{
"id": "1",
"label": "Code 1"
},
{
"id": "4",
"label": "Code 4"
},
{
"id": "5",
"label": "Code 5"
},
{
"id": "2",
"label": "Code 2"
},
{
"id": "3",
"label": "Code 3"
}
],
"approvedCodes": [
"1"
],
"cycles": {
"producer": 21,
"consumer": 21
}
},
"clientForInformation": {
"cycles": {
"producer": 21
}
},
"supplier": {
"codes": [
{
"id": "1",
"label": "Code 1"
},
{
"id": "4",
"label": "Code 4"
},
{
"id": "5",
"label": "Code 5"
},
{
"id": "2",
"label": "Code 2"
},
{
"id": "3",
"label": "Code 3"
}
],
"approvedCodes": [
"1"
],
"cycles": {
"producer": 14,
"consumer": 14
}
},
"supplierIsProducer": {
"supplierRole": "producer"
},
"supplierIsConsumer": {
"supplierRole": "consumer"
}
},
"sequences": {
"$salesPurchase": {
"label": "Client RFQ to OC",
"settings": "supplierIsProducer",
"group": "inquiry"
},
"$salesPurchaseSupplier": {
"label": "Supplier RFQ to OC",
"settings": "supplierIsConsumer",
"group": "inquiry"
},
"$salesClient": null,
"$salesClientFrom": null,
"$client": {
"label": "To client",
"group": "order-fulfillment",
"order": [
"IFR",
"IFI"
],
"stages": {
"IFR": {
"label": "Issued for Review",
"phase": "forApproval",
"settings": "client"
},
"IFI": {
"label": "Issued for Information",
"phase": "forInformation",
"settings": "clientForInformation"
}
},
"interpret": {
"type": "unordered"
}
},
"$supplier": {
"label": "From supplier",
"group": "order-fulfillment-supplier",
"settings": "supplier"
},
"$supplierTo": {
"label": "To supplier",
"group": "order-fulfillment-supplier",
"settings": "supplierIsConsumer"
},
"$internal": null
}
},
"officialMailIdFormat": "M-100-0001",
"transmittalMailIdFormat": "TR-100-0001",
"commercialMailIdFormat": "Bid-100-0001",
"officialMailIdFormats": [
{
"label": "Official",
"format": "M-100-0001"
},
{
"label": "Commercial",
"format": "Bid-100-0001"
}
],
"orderNumber": "100"
},
"projectType": "commercial"
}
}
}
Possible options are: 1) Using OPENJSON() twice (with default and explicit schema) and an additional APPLY operator or 2) Using OPENJSON() (with default schema) and JSON_VALUE():
JSON:
DECLARE #json nvarchar(max) = N'{
"projects":{
"project1id":{
"data":{
"customer":"Cust1",
"name":"Project Name 1"
},
"projectType":"type0"
},
"project2id":{
"data":{
"customer":"Customer 2",
"name":"Name 2",
"projectManager":"Man Ager"
},
"projectType":"type2"
},
"Project3id":{
"data":{
"customer":"Another Customer",
"name":"Another Project"
},
"projectType":"type1"
}
}
}'
Statement with OPENJSON() and APPLY operator:
SELECT j1.[key] AS projectId, j2.*
FROM OPENJSON(#json, '$.projects') j1
CROSS APPLY OPENJSON(j1.[value], '$') WITH (
name nvarchar(100) '$.data.name',
customer nvarchar(100) '$.data.customer',
projectType nvarchar(100) '$.projectType',
projectManager nvarchar(100) '$.data.projectManager'
) j2
Statement with OPENJSON() and JSON_VALUE():
SELECT
projectId = [key],
name = JSON_VALUE([value], '$.data.name'),
customer = JSON_VALUE([value], '$.data.customer'),
projectType = JSON_VALUE([value], '$.projectType'),
projectManager = JSON_VALUE([value], '$.data.projectManager')
FROM OPENJSON(#json, '$.projects')

Bot duplicates user messages when I use DirectLine channel

I am using bot framework v4. I have developed a chatbot using .Net Core. The bot is integrated with LUIS and Qna Maker. One issue I am facing is that the bot duplicates the message that comes from the user. Please look at the screenshot below:
The replies I get from the bot are perfectly fine. The flow of the bot is as intended. I just cannot figure out why the message from user is being duplicated. I am using DirectLine for this. I will share whatever code part is needed.
{
"activities": [
{
"type": "message",
"id": "BR1wBZw7w2852JMLobk0EC-o|0000000",
"timestamp": "2019-12-02T19:41:57.1284328Z",
"channelId": "directline",
"from": {
"id": "CivicDevBot",
"name": "CivicChat"
},
"conversation": {
"id": "BR1wBZw7w2852JMLobk0EC-o"
},
"attachments": [
{
"contentType": "application/vnd.microsoft.card.adaptive",
"content": {
"type": "AdaptiveCard",
"version": "1.0",
"body": [
{
"type": "Container",
"items": [
{
"type": "ColumnSet",
"columns": [
{
"type": "Column",
"width": "auto",
"items": [
{
"type": "Image",
"size": "large",
"url": "https://i.imgur.com/ViaEUnA.png"
}
]
}
],
"horizontalAlignment": "Center"
},
{
"type": "ColumnSet",
"columns": [
{
"type": "Column",
"width": "auto",
"items": [
{
"type": "TextBlock",
"size": "large",
"weight": "bolder",
"color": "light",
"text": "Welcome to CIVIC Financial Services",
"wrap": true
},
{
"type": "TextBlock",
"size": "large",
"weight": "bolder",
"color": "light",
"text": "I am S.U.E",
"wrap": true
},
{
"type": "TextBlock",
"color": "light",
"text": "I can help you answer your questions. Familiarize yourself with CIVIC Financial Services.",
"wrap": true
},
{
"type": "TextBlock",
"color": "light",
"text": "If you want to talk to a Customer Service Agent, just type \"I want to talk to a Customer Service Agent\".",
"wrap": true
}
],
"separator": true,
"horizontalAlignment": "Left"
}
]
}
]
}
],
"actions": [
{
"type": "Action.Submit",
"data": {
"action": "aboutCivic"
},
"title": "About CIVIC"
},
{
"type": "Action.ShowCard",
"card": {
"type": "AdaptiveCard",
"actions": [
{
"type": "Action.ShowCard",
"card": {
"type": "AdaptiveCard",
"body": [
{
"type": "TextBlock",
"text": "We are open from Monday through Friday from 8:00am to 6:00pm.",
"wrap": true
}
],
"style": "emphasis"
},
"title": "When are you open?"
},
{
"type": "Action.ShowCard",
"card": {
"type": "AdaptiveCard",
"body": [
{
"type": "Image",
"size": "stretch",
"url": "https://i.imgur.com/gBVgI25.png",
"horizontalAlignment": "center"
},
{
"type": "TextBlock",
"text": "AZ, CA, CO, FL, GA, HI, NC, NV, OR, SC, TN, TX, UT, VA & WA",
"wrap": true
}
],
"style": "emphasis"
},
"title": "Do you have an office near me? "
},
{
"type": "Action.ShowCard",
"card": {
"type": "AdaptiveCard",
"body": [
{
"type": "TextBlock",
"text": "8 to 10 days, it all depends on how it takes to get access to the property.",
"wrap": true
}
],
"style": "emphasis"
},
"title": "How quickly can we close? "
}
],
"style": "emphasis"
},
"title": "FAQs"
}
]
}
}
],
"entities": [],
"replyToId": "8WGOnspSxN3"
},
{
"type": "message",
"id": "BR1wBZw7w2852JMLobk0EC-o|0000001",
"timestamp": "2019-12-02T19:43:39.96502Z",
"serviceUrl": "https://directline.botframework.com/",
"channelId": "directline",
"from": {
"id": "r_1575315715",
"name": "",
"role": "user"
},
"conversation": {
"id": "BR1wBZw7w2852JMLobk0EC-o"
},
"textFormat": "plain",
"locale": "en-GB",
"text": "teeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
"entities": [
{
"type": "ClientCapabilities",
"requiresBotState": true,
"supportsListening": true,
"supportsTts": true
}
],
"channelData": {
"siteDomain": "https://websiteae1.stackblitz.io/"
}
},
{
"type": "message",
"id": "BR1wBZw7w2852JMLobk0EC-o|0000002",
"timestamp": "2019-12-02T19:43:41.7278914Z",
"channelId": "directline",
"from": {
"id": "CivicDevBot",
"name": "CivicChat"
},
"conversation": {
"id": "BR1wBZw7w2852JMLobk0EC-o"
},
"text": "Sorry, I didn't understand. Consider rephrasing your question or contacting a customer agent",
"attachments": [],
"entities": [],
"replyToId": "BR1wBZw7w2852JMLobk0EC-o|0000001"
},
{
"type": "message",
"id": "BR1wBZw7w2852JMLobk0EC-o|0000003",
"timestamp": "2019-12-02T20:00:25.784598Z",
"serviceUrl": "https://directline.botframework.com/",
"channelId": "directline",
"from": {
"id": "r_1575315715",
"name": "",
"role": "user"
},
"conversation": {
"id": "BR1wBZw7w2852JMLobk0EC-o"
},
"textFormat": "plain",
"locale": "en-GB",
"text": "helo",
"channelData": {
"siteDomain": "https://websiteae1.stackblitz.io/"
}
},
{
"type": "message",
"id": "BR1wBZw7w2852JMLobk0EC-o|0000004",
"timestamp": "2019-12-02T20:00:26.5739342Z",
"channelId": "directline",
"from": {
"id": "CivicDevBot",
"name": "CivicChat"
},
"conversation": {
"id": "BR1wBZw7w2852JMLobk0EC-o"
},
"text": "Sorry, I didn't understand. Consider rephrasing your question or contacting a customer agent",
"attachments": [],
"entities": [],
"replyToId": "BR1wBZw7w2852JMLobk0EC-o|0000003"
},
{
"type": "message",
"id": "BR1wBZw7w2852JMLobk0EC-o|0000005",
"timestamp": "2019-12-02T20:00:27.3293896Z",
"serviceUrl": "https://directline.botframework.com/",
"channelId": "directline",
"from": {
"id": "r_1575315715",
"name": "",
"role": "user"
},
"conversation": {
"id": "BR1wBZw7w2852JMLobk0EC-o"
},
"textFormat": "plain",
"locale": "en-GB",
"text": "hello",
"channelData": {
"siteDomain": "https://websiteae1.stackblitz.io/"
}
},
{
"type": "message",
"id": "BR1wBZw7w2852JMLobk0EC-o|0000006",
"timestamp": "2019-12-02T20:00:27.6064185Z",
"channelId": "directline",
"from": {
"id": "CivicDevBot",
"name": "CivicChat"
},
"conversation": {
"id": "BR1wBZw7w2852JMLobk0EC-o"
},
"text": "Hello",
"inputHint": "acceptingInput",
"attachments": [],
"entities": [],
"replyToId": "BR1wBZw7w2852JMLobk0EC-o|0000005"
}
],
"watermark": "6"
}

How to expand tree node without ajax request?

I am using Extjs tree, i got problem with click on nested node ajax request to server, i know this kind of lazy load but my tree is not big, around 35 items so i want request to server time only to collect all tree node and items.
Any idea?
View:
Ext.define('CRM.view.role.RoleTree' ,{
extend: 'Ext.tree.Panel',
alias: 'widget.roleTreeList',
id:'tree-role-panel',
split: false,
autoScroll: true,
lines: true,
singleExpand: false,
store: 'RoleList'
})
Store:
Ext.define('CRM.store.RoleList', {
extend: 'Ext.data.TreeStore',
storeId: 'RoleTreeStoreId',
//model: 'CRM.model.RoleList',
autoLoad: false,
proxy: {
type : 'ajax',
url : 'index.php/role/getRoleList',
async : false,
reader: {
type : 'json',
method : 'POST'
}
},
root:{
expanded:true,
text:"Organization",
leaf:'false',
id : '/',
}
})
Json return:
{
"text": "Organization",
"id": "role-1",
"children": [
{
"text": "Admin",
"id": "role-12",
"expanded": "true",
"children": [
{
"text": "MSD",
"id": "role-23",
"children": [
{
"text": "Customer Care Manager",
"id": "role-4",
"children": [
{
"text": "Customer Care Adviser",
"id": "role-5"
}
]
},
{
"text": "Solution Adviser",
"id": "role-7"
},
{
"text": "Sales Manager",
"id": "role-31",
"children": [
{
"text": "Solution Sales",
"id": "role-6"
},
{
"text": "Teritory 01 - Safy",
"id": "role-9",
"children": [
{
"text": "Sales Person 01",
"id": "role-10"
},
{
"text": "Sales Trainee 01",
"id": "role-26"
}
]
},
{
"text": "Teritory 3 - Sambo",
"id": "role-11",
"children": [
{
"text": "Sales Person 3",
"id": "role-13"
}
]
},
{
"text": "Teritory 4 - Narong",
"id": "role-14",
"children": [
{
"text": "Sales Team 4",
"id": "role-15",
"children": [
{
"text": "Sales Person 4",
"id": "role-25"
}
]
}
]
},
{
"text": "branch shv",
"id": "role-16",
"children": [
{
"text": "sales shv",
"id": "role-19"
}
]
},
{
"text": "branch btb",
"id": "role-17",
"children": [
{
"text": "sales btb",
"id": "role-20"
}
]
},
{
"text": "branch srp",
"id": "role-18",
"children": [
{
"text": "sales srp",
"id": "role-21",
"children": [
{
"text": "sales person srp",
"id": "role-28"
}
]
}
]
},
{
"text": "Retention Program",
"id": "role-22",
"children": [
{
"text": "Retention Program Analysist",
"id": "role-24"
}
]
},
{
"text": "Sales Trainee",
"id": "role-27"
},
{
"text": "Enterprise Sales Engineer",
"id": "role-29"
},
{
"text": "Meas Test",
"id": "role-30"
},
{
"text": "Teritory 02 - Kol",
"id": "role-32",
"children": [
{
"text": "Sales Person 02",
"id": "role-8"
}
]
}
]
}
]
},
{
"text": "CTO",
"id": "role-35",
"children": [
{
"text": "Support Manager",
"id": "role-2",
"children": [
{
"text": "Technical Support Adviser",
"id": "role-3"
}
]
},
{
"text": "IT BIlling Product Manager",
"id": "role-33",
"children": [
{
"text": "IT Staff",
"id": "role-34"
}
]
}
]
},
{
"text": "Business Consultant",
"id": "role-36"
}
]
}
]
}
You can still use asynchronous call, but only once. To make sure only one call is made, return all children in first call. Extjs will load all children provided.
I found my mistake with JSON, i should add property "leaf = true/false" according has child or hasn't.