【问题标题】:How to convert nested JSON payload from Kebab case into Camel Case using javascript如何使用 javascript 将嵌套的 JSON 负载从 Kebab 大小写转换为 Camel Case
【发布时间】:2022-12-05 20:09:56
【问题描述】:

我需要使用嵌套有效负载将烤肉串转换为驼峰式。示例请求如下。

   "is-success": true,
   "response-date": "2019-02-20T11:42:11.963Z",
   "result": {
       "record-count": "abc123",
       "bill-details-list": [
           {
               "source-system": "Abc123",
               "bill-info": {
                   "bill-amount": "Abc123"
               }
           },
           {
               "SourceSystem": "abc123",
               "bill-info": {
                   "bill-amount-po": "Abc123"
               }
           }
       ]
   }
};

JSON 对象的深度最多可以有 5 个子元素。当我尝试从以下链接实施时 https://gist.github.com/sibu-github/a08c084ff64369f2f6a2b1ae6ee7f10d 输出不符合预期。 当前输出:

{
  isSuccess: true,
  responseDate: '2019-02-20T11:42:11.963Z',
  result: { recordCount: 'abc123', 'billDetailsList': [ [Object], [Object] ] }
}

预期输出:

{
    "isSuccess": true,
    "responseDate": "2019-02-20T11:42:11.963Z",
    "result": {
        "recordCount": "abc123",
        "billDetailsList": [
            {
                "sourceSystem": "Abc123",
                "billInfo": {
                    "billAmountPo": "Abc123"
                }
            },
            {
                "sourceSystem": "abc123",
                "billInfo": {
                    "billAmountPo": "Abc123"
                }
            }
        ]
    }
};

你能提供你的意见吗?

【问题讨论】:

    标签: javascript json camelcasing kebab-case


    【解决方案1】:

    要将 JSON 对象中的 kebab 大小写键转换为驼峰式大小写,您可以使用遍历对象并转换每个嵌套对象的键的递归函数。

    这是执行此操作的函数示例:

    function convertKeysToCamelCase(obj) {
      // If the object is not an object or is null, return it as is
      if (typeof obj !== 'object' || obj === null) {
        return obj;
      }
    
      // If the object is an array, convert each element in the array
      if (Array.isArray(obj)) {
        return obj.map(convertKeysToCamelCase);
      }
    
      // Convert the object keys to camel case
      const converted = {};
      Object.keys(obj).forEach(key => {
        const camelCaseKey = key.replace(/-([a-z])/g, (match, p1) => p1.toUpperCase());
        converted[camelCaseKey] = convertKeysToCamelCase(obj[key]);
      });
    
      return converted;
    }
    

    要使用此函数,您可以将要转换的 JSON 对象作为参数传递给函数,如下所示:

    const json = {
      "is-success": true,
      "response-date": "2019-02-20T11:42:11.963Z",
      "result": {
        "record-count": "abc123",
        "bill-details-list": [
          {
            "source-system": "Abc123",
            "bill-info": {
              "bill-amount": "Abc123"
            }
          },
          {
            "SourceSystem": "abc123",
            "bill-info": {
              "bill-amount-po": "Abc123"
            }
          }
        ]
      }
    };
    const converted = convertKeysToCamelCase(json);
    

    这将产生以下输出:

    {
      "isSuccess": true,
      "responseDate": "2019-02-20T11:42:11.963Z",
      "result": {
        "recordCount": "abc123",
        "billDetailsList": [
          {
            "sourceSystem": "Abc123",
            "billInfo": {
              "billAmount": "Abc123"
            }
          },
          {
            "SourceSystem": "abc123",
            "billInfo": {
              "billAmountPo": "Abc123"
            }
          }
        ]
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-01-25
      • 2019-12-24
      • 2019-07-06
      • 1970-01-01
      • 1970-01-01
      • 2022-07-08
      • 1970-01-01
      • 2011-05-08
      • 2010-11-15
      相关资源
      最近更新 更多