0

I have a JSON object. I want to transform array to produce following as PascalCase:

contacts: [{ givenName: "Matt", familyName: "Berry" }]

Contacts: [{ GivenName: "Matt", FamilyName: "Berry" }]

The following answer did not work

Does Lodash have a Pascal case function? It did not locate it in runtime.

_.mapKeys(obj, (v, k) => _.camelCase(k))

Currently using the Typescript Angular 10 library .

Convert returned JSON Object Properties to (lower first) camelCase

Looking to loop through all members of Array of object,

4 Answers4

1

PascalCase seems to be almost the same as camelCase, only difference being the first letter is capitalized in PascalCase. So you should be able to use the camelCase function from lodash and then just capitalize the first letter of the camelCase word with another function, e.g. something like this:

const capitalize = (str) => {
  return str.charAt(0).toUpperCase() + str.slice(1)
}

So the whole solution is probably something like this:

_.mapKeys(obj, (v, k) => capitalize(_.camelCase(k)))
Pete
  • 280
  • 3
  • 7
  • does this go through all the members of a class jsonobject? –  Nov 22 '20 at 19:33
  • You will need to use the mapkeys function or one of the answers in the other stackoverflow question you linked. I edited a possible solution in my answer, haven't tested it tho – Pete Nov 22 '20 at 19:41
  • I just gave points and accepted answer, feel free to thumbs up question, thanks –  Nov 25 '20 at 18:02
1

You can do it like this;

import {camelCase, startCase} from 'lodash';

startCase(camelCase("givenName")).replace(/ /g, ''); // will procude GivenName

Check this gist for all other scenarios.

If you want to convert all properties of an object, you can use this;

const pascalizeKeys = (obj) => {
  if (Array.isArray(obj)) {
    return obj.map(v => pascalizeKeys(v));
  } else if (obj !== null && obj.constructor === Object) {
    return Object.keys(obj).reduce(
      (result, key) => ({
        ...result,
        [toPascalCase(key)]: pascalizeKeys(obj[key]),
      }),
      {},
    );
  }
  return obj;
};

const toPascalCase = (str) => startCase(camelCase(str)).replace(/ /g, '')

Code taken from this answer and modified further for pascalization.

Here is a stackblitz, so you can inspect further.

tozlu
  • 4,667
  • 3
  • 30
  • 44
  • thanks, one last thing, how do I get function to work on array of objects? its getting error at [0] –  Nov 22 '20 at 19:45
0

Use json-case-convertor

const jcc = require('json-case-convertor')
const pascalCaseJson = jcc.pascalCaseKeys(yourJsonObject)

Link to package: https://www.npmjs.com/package/json-case-convertor

SWIK
  • 714
  • 7
  • 12
0

you can also try this without using lodash.

const contact = {
  givenName: "Matt",
  familyName: "Berry"
};

// define function that converts an object keys to Pascal using the reduce method

const convertToPascalObjectKey = (obj) => {
  return Object.entries(obj).reduce((acc, [key, value]) => {

    // covert the 1st leter of each key to uppercase to create pascalcase
    const pascalCaseKey = key.charAt(0).toUpperCase() + key.slice(1);

    return { ...acc, [pascalCaseKey]: value };

  }, {});
};


const pascalCaseObj = convertToPascalObjectKey(contact);

console.log(pascalCaseObj);
blueelite
  • 319
  • 2
  • 7