简体   繁体   English

从snake_case修改json密钥大小写为camelCase的简单方法

[英]Simple method to modify json keys casing to camelCase from snake_case

I have lots of json parsing and all the json I am receiving is having keys in snake case like user_name . 我有很多json解析,并且我收到的所有json都具有像user_name这样的蛇形键。 And for parsing I need it in camel case like userName . 对于解析,我需要骆驼的情况下,例如userName

The sample json file would look like this: 样本json文件如下所示:

[{
   "user_id" : 1,
   "user_name" : "Abcd"
},
{
   "org_id" : 11,
   "org_name" : "some_name"
}
...
]

Expected output: 预期产量:

[{
   "userId" : 1,
   "userName" : "Abcd"
},
{
   "orgId" : 11,
   "orgName" : "some_name"
}
...
]

The json I am receiving is not having any particular fixed notation and it can be anything. 我收到的json没有任何特定的固定符号,可以是任何东西。 But all the keys will be in snake case. 但是所有的钥匙都在蛇形的情况下。 And I need to convert it to camelCase. 我需要将其转换为camelCase。

What I cannot do is, find and replace, because it also replace snake casing strings in values as well. 我不能做的是,找到并替换,因为它也替换了值中的蛇形套管字符串。

Is there any easy method, which can do the same? 有没有简单的方法可以做到?

You can use npm package called: camelcase-keys-deep 您可以使用名为: camelcase-keys-deep npm软件包。

https://www.npmjs.com/package/camelcase-keys-deep https://www.npmjs.com/package/camelcase-keys-deep

You can do the following: 您可以执行以下操作:

var keys = [];//this will contain the json with desired output
for(var i = 0;i<myObj.length;i++)//myObj is the variable that contains your json
{
    Object.keys(myObj[i]).forEach(function(key){
        if(keys.indexOf(key) == -1)
        {
            var newValue = {};
            var value = myObj[i][key];
            key = key.replace(/_([a-z])/g, function (g) { return g[1].toUpperCase(); });
            newValue[key] = value;
            keys.push(newValue);
        }   
    });

}
//console.log(keys);

Hope this helps :) 希望这可以帮助 :)

 var arr = [{ "user_id": 1, "user_name": "Abcd" }, { "org_id": 11, "org_name": "some_name" } ]; arr.forEach(a => { Object.keys(a).forEach(k => { newK = k.replace(/(\\_\\w)/g, (m) => m[1].toUpperCase()); a[newK] = a[k]; delete a[k]; }); }); console.log(arr); 

So AKSHAY JAIN 's implementation is pretty solid but it will delete the properties that are not in snake case. 因此AKSHAY JAIN的实现非常可靠,但是会删除蛇形情况下的属性。 I fixed the implementation. 我修复了实现。

var arr = [{
  "user_id": 1,
  "user_name": "Abcd"
},
{
  "org_id": 11,
"org_name": "some_name"
},
{
  "personId": 12,
  "personName": "otherName"
}];

arr.forEach(a => {
    Object.keys(a).forEach(k => {
      newK = k.replace(/(\_\w)/g, (m) => m[1].toUpperCase());
      if (newK != k) {
        a[newK] = a[k];
        delete a[k];
      }
    });
  });

console.log(arr);

If u use lodash I would suggest my solution for this: 如果您使用lodash,我将为此建议我的解决方案:

snake_case -> camelCase snake_case->骆驼箱

function camelCaseDeep(anything) {
  const thing = _.cloneDeep(anything);

  if (
    _.isEmpty(thing) ||
    (!_.isObject(thing) && !_.isArray(thing))
  ) {
    return thing;
  }

  if (_.isArray(thing)) {
    const arr = thing;
    return arr.map(el => camelCaseDeep(el))
  }

  // thing can be only not empty object here
  const objWithMappedKeys = _.mapKeys(thing, (value, key) => _.camelCase(key));
  const objWithMappedValues = _.mapValues(objWithMappedKeys, value => camelCaseDeep(value));

  return objWithMappedValues;
}

camelCase -> snake_case camelCase-> snake_case

function snakeCaseDeep(anything) {
  const thing = _.cloneDeep(anything);

  if (
    _.isEmpty(thing) ||
    (!_.isObject(thing) && !_.isArray(thing))
  ) {
    return thing;
  }

  if (_.isArray(thing)) {
    const arr = thing;
    return arr.map(el => snakeCaseDeep(el))
  }

  // thing can be only not empty object here
  const objWithMappedKeys = _.mapKeys(thing, (value, key) => _.snakeCase(key));
  const objWithMappedValues = _.mapValues(objWithMappedKeys, value => snakeCaseDeep(value));

  return objWithMappedValues;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM