user6755654 user6755654 - 21 days ago 12
JSON Question

Output in Json format using dataweave

I'm trying to convert the field-value mapping from CSV to Json format,below code is my dataweave code for mapping the fields from CSV and converting it into Json format:

%dw 1.0
%output application/json
---
{

"volume":
[
payload groupBy $.StartDate map ((val,cal) ->
{
StartDate:val.StartDate[0],

rows :
[
{
AccountID : val.AccountID,
ProductID : val.ProductID,
Value : val.Value
}
]
}
)
]
}


Iam getting output as below :-

{

"volume": [
[
{
"StartDate": "8/1/2016",
"AccountID": [
"16482965",
"16482966"
],
"ProductID": [
"12235398476-AR02",
"12235398477-AR03"
],
"Value": [
"1720",
"1722"
]
},
.
.
.


But i want my output to look like below :

{

"volume": [
[
{
"StartDate": "8/1/2016",
"AccountID":"16482965","ProductID":"12235398476-AR02","Value":"1720",
"AccountID":"16482966","ProductID":"12235398477-AR03","Value": "1722"
},
.
.
.


Can anybody please here?

Answer

According to your answer to my question, here is the JSON structure you need to construct:

{
    "volume": [
                {
                    "StartDate": "8/1/2016",
                    "Entries": [
                                {"AccountID":"16482965","ProductID":"12235398476-AR02","Value":"1720"},
                                {"AccountID":"16482966","ProductID":"12235398477-AR03","Value":"1722"}
                               ]
                },
                ...
              ]
     .
     .
     .
}

Note the addition of the "Entries" element to the structure. It will allow you to run through the array by using references like:

...volume[n].Entries[j].AccountID

Hope this makes things clearer for you.

UPDATE: I missed a closing ]. Added now.

Comments