Convert returned JSON Object Properties to (lower first) camelCase

后端 未结 18 2304
忘掉有多难
忘掉有多难 2020-12-04 21:05

I have JSON returned from an API like so:

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

To keep this consistent

18条回答
  •  天命终不由人
    2020-12-04 21:28

    there's a nice npm module for this.. https://www.npmjs.com/package/camelcase-keys

    npm install camelcase-keys
    
    const camelcaseKeys = require( "camelcase-keys" );
    
    camelcaseKeys( { Contacts: [ { GivenName: "Matt", FamilyName: "Berry" } ] }, { deep: true } );
    

    will return...

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

提交回复
热议问题