I have to read a CSV file with 6 different structured formats and convert to XML format. I need a help to read multi-structured CSV file in mule.
Sample input:
01,12345,Cap,01-02-2017
02,12345,subject1, subject2,subject3,subject4,subject5
03,12345,65432,45,ABS
04,12345,ABC,DEF,,
05,12345,5325,ABC,
06,12345,87.9,ASDF,LKJ
06,12345,99,ABC,WERT
Expected Output:
<Root>
<Sample>
<Number>12345</Number>
<B>Cap</B>
<C>01-02-2017</C>
</Sample>
<Example>
<Sub>
<Number>12345</Number>
<S1>subject1</S1>
<S2>subject2</S2>
<S3>subject3</S3>
<S4>subject4</S4>
<S5>subject5</S5>
</Sub>
<Sub1>
<Number>12345</Number>
<A1>65432</A1>
<A2>45</A2>
<A3>ABS</A3>
</Sub1>
<Sub2>
<Number>12345</Number>
<B1>ABC</B1>
<B2>DEF</B2>
<B3/>
</Sub2>
<Sub3>
<Number>12345</Number>
<C1>5325</C1>
<C2>ABC</C2>
</Sub3>
<Sub4>
<Sub_rec>
<Number>12345</Number>
<D1>87.9</D1>
<D2>ASDF</D2>
<D3>LKJ</D3>
</Sub_rec>
<Sub_rec>
<Number>12345</Number>
<D1>99</D1>
<D2>ABC</D2>
<D3>WERT</D3>
</Sub_rec>
</Sub4>
</Example>
</Root>
Assuming data will not change, dataweave transform is
%dw 1.0
%output application/xml
---
root: {
sample: {
Number: payload[0][0]
},
Example: {
sub : {
Number: payload[1][0]
},
sub1 : {
Number: payload[2][0]
},
sub2 : {
Number: payload[3][0]
},
sub3 : {
Number: payload[4][0]
},
sub4 : {
Sub_rec: {
Number: payload[5][0]
},
Sub_rec: {
Number: payload[6][0]
}
}
}
}
Other values can be filled using S1: payload[1][3] and so on.
Second way is using map, though it also assumes data is static, bit more flexble
root: {
sample: {
Number: payload[0][0]
},
Example: {
(payload[1..-2] map ((row, indexOfRow) -> {
Sub : {
Number: row[0]
} when indexOfRow == 0
otherwise {
Number: row[0]
} when indexOfRow == 1
otherwise {
Number: row[0]
} when indexOfRow == 2
otherwise {
Number: row[0]
} when indexOfRow == 3
otherwise {
(payload[-2..-1] map {
sub_rec: {
Number: $[2]
}
})
}
}))
}
}
Related
I have a project in QML, MomenticsID (BlackBerry10).
I need to use the variable for json answer.
I can not find some manual for this problem.
When I use var result = data.item_01[0] everything is OK and program results in correct value.
But when I use var result = data.variable I won't get an answer.
main.qml
import bb.cascades 1.4
import bb.data 1.0
Page {
id: page
property string variable: "item_01[0]"
Container {
Label {
id: label
}
}
attachedObjects: [
DataSource {
id: dataSource
type: DataSourceType.Json
source: "asset:///data.json"
onDataLoaded: {
var result = data.item_01[0] // it's OK, result is "a"
//var result = data.variable // it's NOK, nothing result
label.text = result.answer_01
}
}
]
onCreationCompleted: {
dataSource.load()
}
}
data.json
{
"item_01": [
{
"answer_01": "a"
}
]
}
Wrong mutation of nested object, the uom_id gets [ null, null ] and it should always have a value.
case ADD_TO_CART:
console.log('ADD TO CART', {...(JSON.parse(JSON.stringify(payload.product)))})
const ex = state.shoppingCart[`c_${payload.product.default_code}`] ? {
...(JSON.parse(JSON.stringify(state.shoppingCart[`c_${payload.product.default_code}`]))),
quantity: state.shoppingCart[`c_${payload.product.default_code}`].quantity + 1,
uom_id: [ payload.product.uom_id[0], payload.product.uom_id[1]],
section: payload.section
} : {
...(JSON.parse(JSON.stringify(payload.product))),
section: payload.section,
uom_id: [ payload.product.uom_id[0], payload.product.uom_id[1]] ,
quantity: 1
};
console.log('THIS IS THE EX, ', ex);
return {
...state,
shoppingCart: {
...state.shoppingCart,
[`c_${payload.product.default_code}`]: state.shoppingCart[`c_${payload.product.default_code}`] ? {
...(JSON.parse(JSON.stringify(state.shoppingCart[`c_${payload.product.default_code}`]))),
quantity: state.shoppingCart[`c_${payload.product.default_code}`].quantity + 1,
uom_id: [ payload.product.uom_id[0], payload.product.uom_id[1]],
section: payload.section
} : {
...(JSON.parse(JSON.stringify(payload.product))),
section: payload.section,
uom_id: [ payload.product.uom_id[0], payload.product.uom_id[1]],
quantity: 1
},
}
}
I expect the object be copied with nested levels.
ct in the image you can see the first object as the payload and after the result of ex
I think your payload object has a product object within a product object.
As such:
{
payload : {
product : {
product : {
},
section: undefined
}
}
}
Therefore you would have to use payload.product.product.uom_id[0] to get your values.
Also, I recommend using deconstructing assignments for objects and arrays to help clean up the code structure. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
Eg.
const { product: { product } } = payload
where this product variable would be the same as payload.product.product
I have filter on my component
filters: {
formatStatus (value) {
let status = {
active: {
text: this.$t('general.successful'),
class: 'secondary--text'
},
processing: {
text: this.$t('general.processing'),
class: 'tertiary--text'
},
waiting: {
text: this.$t('general.pending'),
class: 'tertiary--text'
}
}
return status[value]
}
}
but i got an error
TypeError: Cannot read property '$t' of undefined
but on methods,
$t is working fine.
Rearraing your code like this. it will work
filters: {
formatStatus (value,self) {
let status = {
active: {
text: self.$t('general.successful'),
class: 'secondary--text'
},
processing: {
text: self.$t('general.processing'),
class: 'tertiary--text'
},
waiting: {
text: self.$t('general.pending'),
class: 'tertiary--text'
}
}
return status[value]
}
}
call filter like :
{{yourvalue | formatStatus(this)}}
i am new to mule and i have to convert json to xml.here is the transform output code
%dw 1.0
%input payload application/json
%output application/xml
---
{(
items:
{
Id:payload.abc.productid,
ProductPrice:payload.abc.price,
Name:payload.abc.productname
}
)}
Here is the schema against which i am testing
{
"abc":
{
"productid":"4",
"productname":"Shampoo",
"price":["1000","2000","3000"]
}
}
It generates correct xml in case i send only one item in array price but in case i send multiple elements it gives me this error
Cannot coerce a :array to a :object.
I know i should loop inside it but i do not know how to make it possible
Please guide!
You could use the following:
{
items: {
(payload map ((payload01 , indexOfPayload01) -> {
item: {
Id: payload01.productid,
Name: payload01.productname,
(payload01.price map ((price , indexOfPrice) -> {
price: price
}))
}
}))
}
}
I have removed the "abc" from the incoming data so the format is as:
[
{
"productid":"4",
"productname":"Shampoo",
"price":["1000","2000","3000"]
},
{
"productid":"4",
"productname":"not shampoo",
"price":["1000","2000","3000"]
}
]
Let me know if the abc needs to be there, but I hope this helps
you can use the map keyword to iterate over arrays in Dataweave. Take a look at the documentation here.
I'm using a dc.js datatable and I'd like to add a search box and a pagination by using dataTables.net to filter data in my dataTable.
What I have now ?
In my js file I have this :
var datatable = $('#dc-data-table');
Then the definition of my datatable :
dc.dataTable('.dc-data-table')
.dimension(Mydimension)
.group(function(d) {
// some code;
})
.size(dataSet.length)
.columns([
{
label: '<B>'+'column1'+'</B>',
format: function (d) {
return // some code;
}
},
{
label: '<B>'+'column2'+'</B>',
format: function (d) {
return // some code;
}
},
{
label: '<B>'+'column3'+'</B>',
format: function (d) {
return // some code;
}
},
{
label: '<B>'+'column4'+'</B>',
format: function (d) {
return // some code;
}
},
{
label: '<B>'+'column5'+'</B>',
format: function (d) {
return // some code;
}
},
{
label: '<B>'+'column6'+'</B>',
format: function (d) {
return // some code;
}
},
{
label: '<B>'+'column7'+'</B>',
format: function (d) {
return // some code;
}
},
{
label: '<B>'+'column8'+'</B>',
format: function (d) {
return // some code;
}
},
{
label: '<B>'+'column9'+'</B>',
format: function (d)
{
return // some code;
}
},
{
label: '<B>'+'column10'+'</B>',
format: function (d) {
return // some code;
}
}
])
.sortBy(function (d) {
// some code;
})
.order(d3.ascending)
.renderlet(function (table) {
table.selectAll('.dc-table-group').classed('info', true);
});
In my Html file I have this :
<table class="table table-hover dc-data-table"> </table>
And I have linked two css files bootstrap.min.css and dc.css :
The result :
What I want :
As you can see, I have many rows, I want to active pagination and have a search box to filter data like in DataTables.net examples.
Thank's in advance;