Home > Mobile >  How to convert nested JSON payload from Kebab case into Camel Case using javascript
How to convert nested JSON payload from Kebab case into Camel Case using javascript

Time:12-06

I have a requirement to convert the kebab case into camel case using nested payload.Sample request is as below.

   "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"
               }
           }
       ]
   }
};

The depth of the JSON object can be up to 5 child elements. While I tried to implement from the below linkhttps://gist.github.com/sibu-github/a08c084ff64369f2f6a2b1ae6ee7f10d The output is not as expected. Current Output:

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

ExpectedOutput:

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

Can you please provide your inputs.

CodePudding user response:

To convert the kebab case keys in a JSON object to camel case, you can use a recursive function that traverses the object and converts the keys of each nested object.

Here is an example of a function that does this:

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;
}

To use this function, you can pass the JSON object to convert as the parameter to the function, like this:

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);

This will produce the following output:

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