properJSONify
Goes through a javascript object (recursively) and transforms every key into it's camelCase
version (also takes a custom key transformation function), and sorts keys alphabetically (also takes a custom compare function).
More often than not you'll have to work with a service that outputs { "ThingsLike": "this" }
or { "things_like": "this" }
or even god forbid { "things.like": "this" }
. When that happens,
use properJSONify
.
Installation
yarn add properJSONify
API
Example
const properJSONify = ;const obj = WordsAndWords: 1 nested_object: 'weird.key': 2 ; ;/* Output { wordsAndWords: 1, nestedObject: { weirdKey: 2, }, }*/
Custom key transformation
If camelCase is not what you're after then just pass a transformation function as the
second argument to properJSONify. For instance if you want to prefix all keys with
_
, you would do:
;/* Output: { _a: 1 }*/
Custom key sorting
By default properJSONify
will sort object keys alphabetically,
but you can also use a custom sort function that will be passed to the normal array sort method:
;/* Output: { y: 2, z: 1 }*/
Features
- Traverses both objects and arrays recursively;
- Does not mutate the original object (tests use deepFreeze to be sure);
- Allows custom key transformation functions;
- Prefixes a key with
_
in case of an existing duplicate;