【问题标题】:Change key of a JSON document更改 JSON 文档的键
【发布时间】:2019-03-11 01:13:27
【问题描述】:

我在 MongoDB 中插入了以下 JSON 文件

"entities": [{
    "type": "Location",
    "text": "Marília",
    "relevance": 0.966306,
    "disambiguation": {
      "subtype": [
        "CityTown",
        "City"
      ],
      "name": "Marília",
      "dbpedia_resource": "http://dbpedia.org/resource/Marília"
    },
    "count": 3
  },
  {
    "type": "Organization",
    "text": "Associação Engenheiros, Arquitetos",
    "relevance": 0.647857,
    "count": 1
  }
]

在 MongoDB 中,type 是保留字。如何将type 键更改为desctype?我正在使用 NodeJS。

我已经试过了:

data = JSON.stringify(data).replace(/"type"/g, /"desctype"/); 
data = JSON.parse(data);

但我有错误:

JSON.parse: SyntaxError: Unexpected token / in JSON

【问题讨论】:

    标签: javascript json node.js mongodb mongoose


    【解决方案1】:

    你的语法错误:

    你应该这样使用它:

    data = JSON.stringify(data).replace(/type/g, "desctype"); 
    

    但是,由于您也有 subtype,因此这不会真正起作用。

    更好的方法是映射数据以更改其结构,如下所示:

    data.entities.map(entity => ({ ...entity, desctype: entity.type, type: null }))
    

    ...entity 将传播entity 的所有属性,我们将创建一个desctype,用entity.type 进行初始化,然后将type 设置为null

    【讨论】:

    • 完美运行!非常感谢
    【解决方案2】:

    如果您尝试操作数据结构,我建议您使用对象操作函数来实现此目的。在这里,您可以使用Array.map

    此外,您可以利用 ES6 的方式使用 Arrow functions 简化函数,并使用 Spread Syntax 进一步简化对象操作,它传播对象的所有属性。在这里,函数destructures中的{type, ...rest}对象转化为type,其他属性使用Rest Parameters的概念收集在rest中。

    let entities = [{"type":"Location","text":"Marília","relevance":0.966306,"disambiguation":{"subtype":["CityTown","City"],"name":"Marília","dbpedia_resource":"http://dbpedia.org/resource/Marília"},"count":3},{"type":"Organization","text":"Associação Engenheiros, Arquitetos","relevance":0.647857,"count":1}];
    
    entities = entities.map(({type, ...rest}) => ({...rest, desctype:type}));
    console.log(entities);

    【讨论】:

    • @cale_b - 感谢您的意见。我已更新答案以提供基本见解。我希望它澄清!
    【解决方案3】:

    这会将type 替换为desctype

    let data = {
      "entities": [{
          "type": "Location",
          "text": "Marília",
          "relevance": 0.966306,
          "disambiguation": {
            "subtype": [
              "CityTown",
              "City"
            ],
            "name": "Marília",
            "dbpedia_resource": "http://dbpedia.org/resource/Marília"
          },
          "count": 3
        },
        {
          "type": "Organization",
          "text": "Associação Engenheiros, Arquitetos",
          "relevance": 0.647857,
          "count": 1
        }
      ]
    }
    
    
    data['entities'] = data['entities'].map(function(i){
    
      i['desctype'] = i['type'];
      delete i['type'];
    
      return i
    
    })
    
    console.log(data)

    【讨论】:

      【解决方案4】:

      您可以使用 lodash _.mapKeys 方法。

      const newEntities = entities.map((entity) => {
          _.mapKeys(entity, (value, key) => {
              return (key === 'type') ? 'desctype' : key;
           }
       });
      

      【讨论】:

      • mapKey 方法遍历对象键并返回新键。
      【解决方案5】:

      fileData.findAndCountAll({ 属性,whr }) .then(dta => { 如果 (dta.count > 0) { res.send(dta.rows); } 别的 { var msg = "没有找到记录"; res.status(404).json({ status: false, message: msg }); } });

      【讨论】:

      • 欢迎来到 StackOverflow!请格式化您的代码,并说明您的代码如何提供帮助。
      猜你喜欢
      • 2019-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-20
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      • 2021-09-07
      相关资源
      最近更新 更多