0
votes

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?

2
What you are showing as the desired JSON is not legal (or, at least, inconsistent). You have two instances of AccountID, ProductID and Value. I guess you meant to have one field for StartDate and an array of structures, each structure like one of the records (containing AccountID, ProductID and Value). - FDavidov
Yes @FDavidov - i want all the three fields AccountId,ProductId and value as an array of structures which is group by startdate. - user6755654
I'll add the needed structure as an answer (impossible to do it here). - FDavidov
Sure @FDavidov,Thanks alot - user6755654

2 Answers

0
votes

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.

0
votes

i have used the below code,can you please confirm

     %dw 1.0
     %output application/json
     ---
     {
        "Transaction":"111",
        "type":"b002",
        "volume":
        [       
            payload groupBy $.StartDate map ((val,cal) ->
            {
                StartDate:val.StartDate[0],

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

And iam still getting the out put as :

            {
        "Transaction": "111",
        "type": "b002",
        "volume": [
          [
            {
              "StartDate": "8/1/2016",
              "Entries": [
                {
                  "AccountID": [
                    "16482965",
                    "16482966"
                  ],
                  "ProductID": [
                    "12235398476-AR02",
                    "12235398477-AR03"
                  ],
                  "Value": [
                    "1720",
                    "1722"
                  ]
                }
              ]
            },
            {
              "StartDate": "7/31/2016",
              "Entries": [
                {
                  "AccountID": [
                    "16482964"
                  ],
                  "ProductID": [
                    "12235398475-AR01"
                  ],
                  "Value": [
                    "1720"
                  ]
                }
              ]
            }   

          ]
        ]
      }