Dan Dean
Fork me on GitHub

JSON Transformer Is Rad

Apr 18, 2013

The Problem:

Servers and clients often speak in different JSON variants. The two most frequent use either snake_case or camelCase keys.

It can be frustrating to worked with a mixed bag JSON formats. json-transformer helps with this issue by applying a string transformation to the raw JSON string to enforce either snake_case or camelCase formatting.

A Solution:

With json-transformer, JSON can be transformed before deserialization on the way into your application, and after serialization on the way out. This allows you to work with a consistent JSON format within your application regardless of the preferences of the services your application depends on.

Sample Data

var snake_case_data = JSON.stringify({
  "property_one": "foo",
  "property_two": "bar",
});
// "{"property_one":"foo","property_two":"bar"}"

var camelCaseData = JSON.stringify({
  "propertyOne": "biz",
  "propertyTwo": "baz",
});
// "{"propertyOne":"biz","propertyTwo":"baz"}"

Convert snake_case to camelCase:

var transformer = require('json-transformer');
transformer.underscoreToCamel(snake_case_data);
// {"propertyOne":"foo","propertyTwo":"bar"}

Convert camelCase to snake_case:

transformer.camelToUnderscore(camelCaseData);
// "{"property_one":"biz","property_two":"baz"}"
blog comments powered by Disqus