如何将 JSON 对象名称转换为元素名称?另外,删除嵌套对象?
How does one transform JSON object names as an element name? Also, remove a nested object?
我正在尝试使用 Dataweave 对此进行转换:
{
"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
}
进入这个:
[
{
"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
}
]
此外 - 注意我不想要像 DailyApiRequests 中那样的任何嵌套值
我尝试了地图功能,但不知道如何正确使用。我见过的所有示例似乎都没有显示这种情况。
如果您在 JavaScript 中,您可以使用包 lodash
并使用它的 map
功能:
代码
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)
输出
查看 lodash
文档 here
您可以做的另一件事是 手动遍历对象属性 并自己执行 map,但我认为您最终将重新发明轮子并完全按照 lodash
的方式做 ;) 所以,试一试吧。顺便说一句,那个包太棒了,非常有用。
类似于 Josué 的,但没有引入不必要的依赖项。
const obj = {} // your object above
const res = Object.entries(obj).map( [k, v] => ({
name: k,
...v
}))
另一个例子,但是有一个简单的循环:
const obj = {}; // your object above
const res = []; // New object
for(const [k, v] of Object.entries(obj)) {
res.push({
...v,
name: k
});
}
一个可以在 10 年前的浏览器中运行的示例:
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
});
}
使用 DataWeave
%dw 1.0
%input payload application/json
%output application/json
---
payload pluck ((value,key) -> {
name: key,
max: value.Max,
remaining: value.Remaining
}
)
我正在尝试使用 Dataweave 对此进行转换:
{
"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
}
进入这个:
[
{
"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
}
]
此外 - 注意我不想要像 DailyApiRequests 中那样的任何嵌套值
我尝试了地图功能,但不知道如何正确使用。我见过的所有示例似乎都没有显示这种情况。
如果您在 JavaScript 中,您可以使用包 lodash
并使用它的 map
功能:
代码
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)
输出
查看 lodash
文档 here
您可以做的另一件事是 手动遍历对象属性 并自己执行 map,但我认为您最终将重新发明轮子并完全按照 lodash
的方式做 ;) 所以,试一试吧。顺便说一句,那个包太棒了,非常有用。
类似于 Josué 的,但没有引入不必要的依赖项。
const obj = {} // your object above
const res = Object.entries(obj).map( [k, v] => ({
name: k,
...v
}))
另一个例子,但是有一个简单的循环:
const obj = {}; // your object above
const res = []; // New object
for(const [k, v] of Object.entries(obj)) {
res.push({
...v,
name: k
});
}
一个可以在 10 年前的浏览器中运行的示例:
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
});
}
使用 DataWeave
%dw 1.0
%input payload application/json
%output application/json
---
payload pluck ((value,key) -> {
name: key,
max: value.Max,
remaining: value.Remaining
}
)