title: How to convert JSON string having multiple rows to single row using javascript function
body: <p>I have an output of REST API in following JSON format:
  I need to convert the format to flat format so it can be passed as input to another API call.</p>
  
  <pre><code>{
    "result": {
      "data": [
        {
          "data": 2.824315071105957,
          "dateTime": "2019-09-10T11:32:05.220Z",
          "device": { "id": "b3" },
          "diagnostic": { "id": "DiagnosticAccelerationForwardBrakingId" },
          "controller": "ControllerNoneId",
          "version": "00000000000363b0",
          "id": "a5UyPzhknSC-N2wtLBph3BA"
        },
        {
          "data": 0,
          "dateTime": "2019-09-10T11:32:05.220Z",
          "device": { "id": "b3" },
          "diagnostic": { "id": "DiagnosticAccelerationSideToSideId" },
          "controller": "ControllerNoneId",
          "version": "00000000000363b1",
          "id": "a5UyPzhknSC-N2wtLBph3BQ"
        },
        // ... 1000's of rows like this
      ]
    }
  }
  </code></pre>
  
  <p>I need to convert it in below format using a java-script</p>
  
  <p>Desired format:</p>
  
  <pre><code>{"result":{ "data":[{"id":"b3","dateTime":"2019-09- 10T11:32:05.220Z","DiagnosticAccelerationSideToSideId":0,"DiagnosticAccelerationForwardBrakingId ":2.824315071105957},...
  </code></pre>
  
  <p>The rows needs to be merged with primary key as combination of ID and dateTime attributes. Please note the diagnostic id value becomes key for the required format and data value is the value of the key.</p>
  
  <p>Is there any way to convert this JSON to above flat format.</p>
  
  <p>Need to convert JSON having many rows for single data entry to single row format. Need one java-script function that can accept a string of rows format and convert or merge it and return the string in desired format</p>
  
  <pre><code>function String mergeRows(String flatDataJSONString) {
  ...
  }
  </code></pre>