wankdanker / node-object-mapper

Copy properties from one object to another.
MIT License
276 stars 73 forks source link

JSON Transform Only Returning 1st Item in JSON Object #87

Open ruskspace opened 3 years ago

ruskspace commented 3 years ago

I am using object-mapper to transform a JSON object to a different JSON format. The following works... but only produces the first entry in the JSON I have. Is there some sort of foreach or iteration that needs to be added?

This:

  var src = response.data;

  var map = {
    "value.segments.start": "value.start",
    "value.segments.end": "value.end",
    "value.segments.segments.performanceCounters/processCpuPercentage.avg": "value.cpu"
  };

  var dest = objectMapper(src, map);

  res.send(dest);

Produces this:

{
  "value": {
    "start": "2021-04-15T00:00:00.000Z",
    "end": "2021-04-16T00:00:00.000Z",
    "cpu": 9.01
  }
}

But here is my response.data as I get JSON from one source and need to transform it to another format, as you can see multiple entries and I expected to multiple entries in the object-mapped output:

{
  "value": {
    "start": "2021-04-14T18:17:27.086Z",
    "end": "2021-04-15T18:17:27.086Z",
    "interval": "PT1H",
    "segments": [
      {
        "start": "2021-04-14T18:17:27.086Z",
        "end": "2021-04-14T19:00:00.000Z",
        "segments": [
          {
            "performanceCounters/processCpuPercentage": {
              "avg": 22.9
            },
            "customDimensions/Role": "CD"
          }
        ]
      },
      {
        "start": "2021-04-14T19:00:00.000Z",
        "end": "2021-04-14T20:00:00.000Z",
        "segments": [
          {
            "performanceCounters/processCpuPercentage": {
              "avg": 23.51
            },
            "customDimensions/Role": "CD"
          }
        ]
      },
      {
        "start": "2021-04-15T00:00:00.000Z",
        "end": "2021-04-15T01:00:00.000Z",
        "segments": [
          {
            "performanceCounters/processCpuPercentage": {
              "avg": 8.85
            },
            "customDimensions/Role": "CD"
          }
        ]
      },