2

Using Dataweave I'm attempting to transform this:

{
    "ConcurrentAsyncGetReportInstances": {
        "Max": 200,
        "Remaining": 200
    },
    "ConcurrentSyncReportRuns": {
        "Max": 20,
        "Remaining": 20
    },
    "DailyAnalyticsDataflowJobExecutions": {
        "Max": 50,
        "Remaining": 50
    },
    "DailyApiRequests": {
        "Max": 6175000,
        "Remaining": 6174972,
        "Ant Migration Tool": {
            "Max": 0,
            "Remaining": 0
        },
        "CLM_service_api": {
            "Max": 0,
            "Remaining": 0
        },
        "CRM_service_api": {
            "Max": 0,
            "Remaining": 0
        }
    },
    "DailyAsyncApexExecutions": {
        "Max": 325000,
        "Remaining": 324902
 }

Into this:

[
{
    "name":"ConcurrentAsyncGetReportInstances",
    "max":200,
    "remaining":200 
},
{
    "name":"ConcurrentSyncReportRuns",
    "max":"20",
    "remaining":"20"    
},
{
    "name":"DailyAnalyticsDataflowJobExecutions",
    "max":50,
    "remaining":50  
},
{
    "name":"DailyApiRequests",
    "max":6175000,
    "remaining":6174972 
},
{
    "name":"DailyAsyncApexExecutions",
    "max":325000,
    "remaining":324902  
}
]

Also - note I don't want any nested values like in DailyApiRequests

I have attempted the map function but am uncertain how to properly use. All the examples I have seen dont seem to show this scenario.

3 Answers3

2

Using DataWeave

%dw 1.0
%input payload application/json
%output application/json
---
payload pluck ((value,key) -> {
    name: key,
    max: value.Max,
    remaining: value.Remaining
  } 
)
machaval
  • 4,969
  • 14
  • 20
0

If you are in JavaScript you can use package lodash and use its map function:

Code

const _ = require('lodash')

const obj = ... // your object above
const res = _.map(obj, (v, k) => ({
  name: k, 
  max: v.Max, 
  remaining: v.Remaining
}))

console.log(res)

Output

enter image description here

Look at lodash documentation here

The other thing you can do is to iterate through the object properties by hand and do the map by yourself, but I think you will end up reinventing the wheel and doing exactly what lodash does ;) so, give it a try. By the way, that package is amazing, it is extremely useful.

JoshuaCS
  • 2,524
  • 1
  • 13
  • 16
0

Similar to Josué 's, but without introducing unneeded dependencies.

const obj = {} // your object above
const res = Object.entries(obj).map( [k, v] => ({
  name: k, 
  ...v
}))

Another example, but with a simple loop:

const obj = {}; // your object above
const res = []; // New object
for(const [k, v] of Object.entries(obj)) {
  res.push({
    ...v,
    name: k
  });
}

An example that will work in a 10-year old browser:

var obj = {}; // your object above
var res = []; // New object
for(var key in obj) {
  if (!obj.hasOwnProperty(key)) continue;
  res.push({
    name: k, 
    max: obj[k].Max, 
    remaining: obj[k].Remaining
  });
}
Evert
  • 93,428
  • 18
  • 118
  • 189