Mirthconnector : How to convert nested json to csv using mirth connector - mirth-connect

I need to convert the JSON file to CSV using mirth connector. I am very new to mirth connector could you please help me.
I have created the channel and i have written the below code which is giving my output in single line.
Code:
for each (element in jsonObject)
{
var row = <row/>;
var col = <row/>;
for (var key1 in element)
{if (typeof element[key1] != 'object')
{
row[key1] = element[key1].toString();
col[key1] = key1;
}
for (var key2 in element[key1])
{if (typeof element[key1][key2] != 'object')
{
row[key1+key2] = element[key1][key2].toString();
col[key1+key2] = key1+'.'+key2;
}
}
}
msg.appendChild(col);
msg.appendChild(row);
}
Input:
{
"colors": [
{
"color": "black",
"category": "hue",
"type": "primary",
"code": {
"rgba": [255,255,255,1],
"hex": "#000"
}
},
{
"color": "white",
"category": "value",
"code": {
"rgba": [0,0,0,1],
"hex": "#FFF"
}
},
{
"color": "red",
"category": "hue",
"type": "primary",
"code": {
"rgba": [255,0,0,1],
"hex": "#FF0"
}
},
{
"color": "blue",
"category": "hue",
"type": "primary",
"code": {
"rgba": [0,0,255,1],
"hex": "#00F"
}
},
{
"color": "yellow",
"category": "hue",
"type": "primary",
"code": {
"rgba": [255,255,0,1],
"hex": "#FF0"
}
},
{
"color": "green",
"category": "hue",
"type": "secondary",
"code": {
"rgba": [0,255,0,1],
"hex": "#0F0"
}
}
]
}

Related

How to use filter in map

I have following code:
%dw 2.0 output application/json var attributeIdMapping =
${vault::attributeIdMapping}
--- {
"objectTypeId": 3,
"attributes": vars.requestBody mapObject (value, key) ->
{
"objectTypeAttributeId": (attributeIdMapping.attributes filter ($.name == key))[0].id,
"objectAttributeValues": [{
"value": value,
"key": key
}]
} }
attributeIdMapping:
{
"attributes": [
{
"name": "accountType",
"id": "87"
},
{
"name": "accountClass",
"id": "89"
},
{
"name": "accountName",
"id": "85"
},
{
"name": "displayName",
"id": "18"
},
{
"name": "accountCategory",
"id": "88"
},
{
"name": "accountNumber",
"id": "84"
},
{
"name": "description",
"id": "86"
},
{
"name": "accountGroup",
"id": "90"
}
]
}
vars.requestBody:
{
"displayName": "TestMulesoft2",
"description": "Test"
}
But my filter shows null in the end. As I understand key is not passed to level below map itself. How can I get this working?
I put all the input data inside the script to simplify reproduction. The problem seems to be using the operator == to compare a string and a key types returns empty. Using the operator ~= which attempts to coerce appears to return the expected result:
%dw 2.0
output application/json
var attributeIdMapping =
{
"attributes": [
{
"name": "accountType",
"id": "87"
},
{
"name": "accountClass",
"id": "89"
},
{
"name": "accountName",
"id": "85"
},
{
"name": "displayName",
"id": "18"
},
{
"name": "accountCategory",
"id": "88"
},
{
"name": "accountNumber",
"id": "84"
},
{
"name": "description",
"id": "86"
},
{
"name": "accountGroup",
"id": "90"
}
]
}
var requestBody = {
"displayName": "TestMulesoft2",
"description": "Test"
}
--- {
"objectTypeId": 3,
"attributes": requestBody mapObject (value, key) ->
{
"objectTypeAttributeId": (attributeIdMapping.attributes filter ($.name ~= key))[0].id,
"objectAttributeValues": [{
"value": value,
"key": key
}]
} }
Output:
{
"objectTypeId": 3,
"attributes": {
"objectTypeAttributeId": "18",
"objectAttributeValues": [
{
"value": "TestMulesoft2",
"key": "displayName"
}
],
"objectTypeAttributeId": "86",
"objectAttributeValues": [
{
"value": "Test",
"key": "description"
}
]
}
}

How to get the nested array value in mongodb

Need to get collection based on component, Subtype, material value which is inside regions array.
"_id":"01"
"pole_num": "1"
"pole_height":"10"
"lat":"39.94142507"
"lon": "-86.07079913"
"image": "_DSC9468.JPG",
"road_accessible": "true",
"regions": {
0: {
"Component": "Pole Test",
"Sub Type": {
},
"Material": "Unknown",
"Condition": {
},
"Misc": {
}
},
1: {
"Component": "TestAnchor",
"Sub Type": {
},
"Material": "Unknown",
"Condition": {
},
"Misc": {
}
}
"_id":"02"
"pole_num": "2"
"pole_height":"10"
"lat":"39.94142507"
"lon": "-86.07079913"
"image": "_DSC9468.JPG",
"road_accessible": "true",
"regions": [
0: {
"Component": "Pole Test",
"Sub Type": {
},
"Material": "Unknown",
"Condition": {
},
"Misc": {
}
},
1: {
"Component": "Test Anchor2",
"Sub Type": {
},
"Material": "Unknown",
"Condition": {
},
"Misc": {
}
}
How to get datas which has "Pole Test" value like given below:
"_id":"01"
"pole_num": "1"
"pole_height":"10"
"lat":"39.94142507"
"lon": "-86.07079913"
"image": "_DSC9468.JPG",
"road_accessible": "true",
"regions": {
0: {
"Component": "Pole Test",
"Sub Type": {
},
"Material": "Unknown",
"Condition": {
},
"Misc": {
}
}
and
"_id":"02"
"pole_num": "2"
"pole_height":"10"
"lat":"39.94142507"
"lon": "-86.07079913"
"image": "_DSC9468.JPG",
"road_accessible": "true",
"regions": [
0: {
"Component": "Pole Test",
"Sub Type": {
},
"Material": "Unknown",
"Condition": {
},
"Misc": {
}
}
is there any possible mongodb query to work with this.
Check the link below. Its a very simple query which can be done using find operator.
Unless there is some sample value provided by you in subtype field.
You can update your question so we can provide better solution
db.collection.find({
"regions.Component": "Pole Test",
"regions.Material": "Unknown"
})
https://mongoplayground.net/p/1K4NXPTllHz

JSONata, pulling data from nested objects

I'm new to JSONata and so still getting my head around it. I need to pull data out of a nested object, for example from :
{
"transaction": {
"id": "de112b4b-82e2-4172-a89f-68724c90b692"
},
"domain": {
"id": "realworld"
},
"listing": {
"spanner": {
"information": {
"type": {
"VENDOR": "Charlie"
},
"variables": {
},
"uid": "08_spanner",
"mode": {
"store": "bob"
},
"version": "1",
"name": "Harrold"
}
},
"hammer": {
"information": {
"type": {
"VENDOR": "Cliff"
},
"variables": {
},
"uid": "08_hammer",
"mode": {
"store": "steve"
},
"version": "1",
"name": "Mike"
}
},
"wrench": {
"information": {
"type": {
"VENDOR": "Dave"
},
"variables": {
},
"uid": "08_wrench",
"mode": {
"store": "bob"
},
"version": "1",
"name": "Kent"
}
}
}
}
... I need to pull out the listing data with it's "information", but without the information key. So the result would look like:
{
"spanner": {
"type": {
"VENDOR": "Charlie"
},
"variables": {
},
"uid": "08_spanner",
"mode": {
"store": "bob"
},
"version": "1",
"name": "Harrold"
},
"hammer": {
"type": {
"VENDOR": "Cliff"
},
"variables": {
},
"uid": "08_hammer",
"mode": {
"store": "steve"
},
"version": "1",
"name": "Mike"
},
"wrench": {
"type": {
"VENDOR": "Dave"
},
"variables": {
},
"uid": "08_wrench",
"mode": {
"store": "bob"
},
"version": "1",
"name": "Kent"
}
}
I have been messing around on http://try.jsonata.org/ and can see how powerful the library is but so far no success at achieving this goal. Any and all help is appreciated :).
The following expression will do this:
listing.$each(function($value, $key) {
{ $key: $value.information }
}) ~> $merge()
See http://try.jsonata.org/S17Erm85z

Error when saving composites id inside a for loop

I am trying to persist some objects that have a composite id. If I am only sending an array with one element it works fine, but it the array has more than one it throws an exception when saving the first one. public boolean
addParamsToChart(List<ChartParams> chartParams, Long chartId) {
List<ChartParams> chartParamsList = new ArrayList<>();
for(ChartParams chartParam: chartParams) {
ChartParamsId id = new ChartParamsId();
// id.setChartId(chartId);
id.setChartId(chartParam.getChart().getId());
id.setParamId(chartParam.getParam().getId());
id.setContextSourceId(chartParam.getContextSource().getId());
chartParam.setChartParamsId(id);
if(chartParamsRepository.save(chartParams) !=null) {
chartParamsList.add(chartParam);
}
}
if(chartParamsList.size()!=chartParams.size()) {
// something went wrong, delete previous inserted
deleteChartParams(chartParamsList);
chartRepository.delete(chartId);
return false;
}
return true;
}
[{
"chart": {
"id": 49,
"cv": {
"id": 1,
"name": "Money",
"category": {
"id": 1,
"name": "Euros"
},
"definition": "\"European curreny.\" [EU:euro]",
"enabled": true,
"cvid": "CC:1010"
},
"accountType": {
"id": 1,
"name": "saving"
},
"name": "Euro saving charts"
},
"param": {
"id": 8,
"name": "Totals",
"isFor": "Currency"
},
"contextSource": {
"id": 3,
"name": "euro",
"internal": "eu",
"abbreviatedName": "eu"
} }]
But for this having two objects inside instead of one is not working, throwing an exception at the first save.
[{
"chart": {
"id": 52,
"cv": {
"id": 55,
"name": "Stocks",
"category": {
"id": 1,
"name": "Stocks"
},
"definition": "\"General stocks.\" [GS:ST]",
"enabled": true,
"cvid": "ST:0111"
},
"accountType": {
"id": 1,
"name": "saving"
},
"name": "Stock saving chart"
},
"param": {
"id": 8,
"name": "Totals",
"isFor": "Currency"
},
"contextSource": {
"id": 6,
"name": "stock",
"internal": "st",
"abbreviatedName": "st"
} }, {
"chart": {
"id": 52,
"cv": {
"id": 55,
"name": "Stocks",
"category": {
"id": 1,
"name": "Stocks"
},
"definition": "\"General stocks.\" [GS:ST]",
"enabled": true,
"cvid": "ST:0111"
},
"accountType": {
"id": 1,
"name": "saving"
},
"name": "Stock saving chart"
},
"param": {
"id": 8,
"name": "Totals",
"isFor": "Currency"
},
"contextSource": {
"id": 7,
"name": "Sold stock",
"internal": "st_sold",
"abbreviatedSequence": "st_sold"
} }]
The exception I got is:
org.hibernate.id.IdentifierGenerationException: null id generated for:class eu.stocks.chart.chartParams.ChartParams

Kendo UI Grid Column typed date has a trouble

I have a grid which would have many columns typed date. These all grids is generated from a generic function, so that I cannot know whether the column type is date or not. There is one rule validates through all columns. It is that all columns name end with "Date" suffix. For instance, createDate, editDate, visitedDate, etc...
So that, I can understand that it can be date and I parse it like you can see at the dataSource parse function
I have trouble when I update the cell. It does not reflect its own value to Model. The date column proto function returns "Invalid Date" error. I do not understand what it happen
var dataSource = new kendo.data.DataSource({
"data": [
{
"hidden_gridColumns": "",
"id": "21632",
"projectId": "146",
"customerTypeId": "4",
"district": "0",
"fieldSize": "12",
"fieldType": "0",
"floorCoveringType": "12",
"lastChangeDate": null,
"estimatedModificationDate": null,
"latestCompany": ""
}
],
"schema": {
"model": {
"id": "id",
"fields": {
"gridColumns": {
"type": "string"
},
"id": {
"type": "string"
},
"projectId": {
"type": "string"
},
"customerTypeId": {
"type": "string"
},
"district": {
"type": "string"
},
"fieldSize": {
"type": "string"
},
"fieldType": {
"type": "string"
},
"floorCoveringType": {
"type": "string"
},
"lastChangeDate": {
"type": "date"
},
"estimatedModificationDate": {
"type": "string"
},
"latestCompany": {
"type": "string"
}
}
},
parse: function(data){
$.each(data,
function(rowNo,
row){
$.each(row,
function(colName,
column){
if(colName.indexOf("Date")>=0){
console.log(colName + " taranıyor");
row[colName] = kendo.parseDate(row[colName], "dd-MM-yyyy");
}
});
});
return data;
}
},
"batch": true
})
And this is my columns structure:
var columns = [
{
"title": "gridColumns",
"field": "gridColumns",
"hidden": true
},
{
"title": "id",
"field": "id",
"hidden": true
},
{
"title": "projectId",
"field": "projectId",
"hidden": true
},
{
"title": "Müşteri Tipi",
"field": "customerTypeId",
"hidden": false,
"width": "91",
"values": [
{
"value": "1",
"text": "Yurtiçi "
},
{
"value": "2",
"text": "Yurtdışı"
},
{
"value": "3",
"text": "Spor Kulübü"
},
{
"value": "4",
"text": "Diğer"
},
{
"value": "5",
"text": "Üniversite"
},
{
"value": "6",
"text": ""
}
]
},
{
"title": "district",
"field": "district",
"hidden": true,
"width": "132"
},
{
"title": "Saha Ölçüsü",
"field": "fieldSize",
"hidden": false,
"width": "85"
},
{
"title": "Saha Türü",
"field": "fieldType",
"hidden": false,
"values": [
{
"value": "1",
"text": "Açık"
},
{
"value": "0",
"text": "Kapalı"
}
]
},
{
"title": "Halı Cinsi",
"field": "floorCoveringType",
"hidden": false,
"width": "76"
},
{
"title": "Son Halı değişim Tarih",
"field": "lastChangeDate",
"hidden": false,
"format": "{0:dd-MM-yyyy}"
},
{
"title": "Tahmini Yenileme Tarihih",
"field": "estimatedModificationDate",
"hidden": false
},
{
"title": "Son Çalıştığı Halı Firması",
"field": "latestCompany",
"hidden": false
}
]
I solved this problem by the code below:
parse: function(response) {
var tmpData=[];
for (var i = 0; i < response.length; i++) {
var tmpRow = response[i];
$.each(tmpRow, function(colNo, colValue){
if(colNo.indexOf("Date")>-1){
tmpRow[colNo]=kendo.parseDate(new Date(tmpRow[colNo]));
}
});
tmpData.push(tmpRow);
}
return tmpData;
}

Resources