Node.js 中的 JSON 转换

标签 json node.js transform

我想将数据从一种 json 结构转换为另一种结构。最好的方法是什么?

这是我原来的资源(客户)结构是:

{
  "id": "123",
  "data": {
    "name": "john doe",
    "status": "active",
    "contacts": [
      {
        "email": "john@email.com"
      },
      {
        "phone": "12233333"
      }
    ]
  }
}

我想将其更改为:

{
  "id": "123",
  "name": "john doe",
  "status": "active",
  "contacts": [
    {
      "email": "john@email.com"
    },
    {
      "phone": "12233333"
    }
  ]
}

请记住,在 GET/customers 情况下,我可能会返回一个数组资源(客户)。我想将其更改为新数据类型的数组。

最佳答案

如果客户对象是对象数组,那么下面将帮助您获得所需的格式结果

var result = customerObj.map(x => {
  return {
    id: x.id,
    name: x.data.name,
    status: x.data.status,
    contacts: x.data.contacts
  };
});

关于Node.js 中的 JSON 转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47547197/

相关文章:

json - 从 R 中的加载对象创建 json 字符串

C# 遍历json

node.js - Backbone.js - 在 ajax 中获取 View 、模型或集合属性 'beforeSend'

javascript - 编辑以前保存的文档也会在数据库中更新它

css - CSS卡翻转效果略有不同

python - Pandas,如何找到互补的时间范围?

"type"="string"时的 JSON 模式验证为 null

cookies - 通过 WebSockets 连接到 socket.io 时如何发送 cookie?

swift - 如何在swift中将圆圈变成一条线?

json - Play Framework :JSON读取单个属性的案例类