简体   繁体   中英

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?

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.

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"
                  ]
                }
              ]
            }   

          ]
        ]
      } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM