0

I have to transfrom input json to output json message using javascript Below are input and output test samples Input

[{
   "Id": "123",
   "Address": "12",
   "Cadre": "LOLOL" 
   "Meeting": {"Output":    [
            {
         "Date": "2020-08-13T15:00:00Z",
         "comment": "Working"
      },
            {
         "timestamp": "2020-08-13T10:03:01Z",
         "comment": "Learning"
      },
          
     ]}
        
    },
    {
   "Id": "345",
   "Address": "14",
   "Cadre": "Loop" 
   "Meeting": {"Output":    [
            {
         "Date": "2020-12-12T18:27:00Z",
         "comment": "Working"
      },
            {
         "timestamp": "2020-11-22T14:53:01Z",
         "comment": "Learning"
      },
          
     ]}
        
    }   
]

Output also should remain same, with out changes to other fields except the array in "Meeting/Output/comment" field.

Output

[{
   "Id": "123",
   "Address": "12",
   "Cadre": "LOLOL" 
   "Meeting": {"Output":    [
            {
         "Date": "2020-08-13T15:00:00Z",
         "comment": "NOt known"
      },
            {
         "timestamp": "2020-08-13T10:03:01Z",
         "comment": "Taken"
      },
          
     ]}
        
    },
    {
   "Id": "345",
   "Address": "14",
   "Cadre": "Loop" 
   "Meeting": {"Output":    [
            {
         "Date": "2020-12-12T18:27:00Z",
         **"comment": "Should Not be"**
      },
            {
         "timestamp": "2020-11-22T14:53:01Z",
         **"comment": "Help///A"**
      },
          
     ]}
        
    }   
]

I have taken "for" loop to iterate on Input payload array object and retrieved the "Meeting" object made necessary changes I have tried this

session.input.readAsJSON(function(readAsJSONError, payload) {   
    var incomingDataLength = payload.length;    
    for(var i=0; i<incomingDataLength; i++){        
        var reqArr = payload.Meeting.Output;
        if(reqArr === null || reqArr === '' || reqArr === undefined) {
        console.log("Do something");
        } else {
        var reqArrLength = payload.Meeting.Output.length;
        for(var i=0;  i<reqArrLength;  i++){
            reqArrLength[i].comment= callsomefunction(reqArrLength[i].comment);
        }
        }       
    }

});

Could you please suggest me on this to proceed.

Virat
  • 77
  • 1
  • 5
  • Welcome to Stack Overflow! Please take the [tour] (you get a badge!) and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) Your best bet here is to do your research, [search](/help/searching) for related topics on SO and elsewhere, and give it a go. ***If*** you get stuck and can't get unstuck after doing more research and searching, post a [mcve] showing your attempt and say specifically where you're stuck. People will be glad to help. – T.J. Crowder Oct 06 '21 at 07:51
  • See: https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json Also, JSON is a *textual notation* for data exchange. [(More here.)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. – T.J. Crowder Oct 06 '21 at 07:51
  • Please add the code you have so far and describe exactly how it fails. `but how the changes can I add include in output` sounds like a trivial problem to solve tbh. –  Oct 06 '21 at 07:55

1 Answers1

0

The simplest way to do it is by using Array.map

var output = input;
output.Meeting.Output = input.Meeting.Output.map(item =>
{
  delete item.comment;
  return item;
});

If you already have the array - then use the Array.forEach

input.Meeting.Output.forEach(item =>
{
  delete item.comment;
});
IVO GELOV
  • 13,496
  • 1
  • 17
  • 26
  • Hi IVO GELOV, I have tried using map in my code and seeing Array created again. Using Map function it created Array but in my payload I have the array already. Could you please suggest is there any other way to proceed on this. – Virat Oct 20 '21 at 06:07
  • @Virat On existing arrays you can use `forEach` – IVO GELOV Oct 21 '21 at 11:17