Convert returned JSON Object Properties to (lower first) camelCase

后端 未结 18 2173
忘掉有多难
忘掉有多难 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:26

    Here's a reliable, recursive function that will properly camelCase all of a JavaScript object's properties:

    function toCamel(o) {
      var newO, origKey, newKey, value
      if (o instanceof Array) {
        return o.map(function(value) {
            if (typeof value === "object") {
              value = toCamel(value)
            }
            return value
        })
      } else {
        newO = {}
        for (origKey in o) {
          if (o.hasOwnProperty(origKey)) {
            newKey = (origKey.charAt(0).toLowerCase() + origKey.slice(1) || origKey).toString()
            value = o[origKey]
            if (value instanceof Array || (value !== null && value.constructor === Object)) {
              value = toCamel(value)
            }
            newO[newKey] = value
          }
        }
      }
      return newO
    }
    

    Test:

    var obj = {
      'FirstName': 'John',
      'LastName': 'Smith',
      'BirthDate': new Date(),
      'ArrayTest': ['one', 'TWO', 3],
      'ThisKey': {
        'This-Sub-Key': 42
      }
    }
    
    console.log(JSON.stringify(toCamel(obj)))
    

    Output:

    {
        "firstName":"John",
        "lastName":"Smith",
        "birthDate":"2017-02-13T19:02:09.708Z",
        "arrayTest": [
            "one", 
            "TWO", 
            3
        ],
        "thisKey":{
            "this-Sub-Key":42
        }
    }
    
    0 讨论(0)
  • 2020-12-04 21:28

    This is a great use case for axios interceptors

    Basically, define a client class and attach a before/after interceptor that converts the request/response data.

    export default class Client {
        get(url, data, successCB, catchCB) {
            return this._perform('get', url, data, successCB, catchCB);
        }
    
        post(url, data, successCB, catchCB) {
            return this._perform('post', url, data, successCB, catchCB);
        }
    
        _perform(method, url, data, successCB, catchCB) {
            // https://github.com/axios/axios#interceptors
            // Add a response interceptor
            axios.interceptors.response.use((response) => {
                response.data = toCamelCase(response.data);
                return response;
            }, (error) => {
                error.data = toCamelCase(error.data);
                return Promise.reject(error);
            });
    
            // Add a request interceptor
            axios.interceptors.request.use((config) => {
                config.data = toSnakeCase(config.data);
                return config;
            }, (error) => {
                return Promise.reject(error);
            });
    
            return axios({
                method: method,
                url: API_URL + url,
                data: data,
                headers: {
                    'Content-Type': 'application/json',
                },
            }).then(successCB).catch(catchCB)
        }
    }
    

    Here's a gist with a longer example using React/axios.

    0 讨论(0)
  • 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" } ] }
    
    0 讨论(0)
  • 2020-12-04 21:31

    Updated code using the reference from https://plnkr.co/edit/jtsRo9yU12geH7fkQ0WL?p=preview This handles the Objects with array with objects inside it too and so on, by keeping arrays as arrays (which you can iterate over using map)

    function snakeToCamelCase(snake_case_object){
      var camelCaseObject;
      if (isPlainObject(snake_case_object)) {        
        camelCaseObject = {};
      }else if(isArray(snake_case_object)){
        camelCaseObject = [];
      }
      forEach(
        snake_case_object,
        function(value, key) {
          if (isPlainObject(value) || isArray(value)) {
            value = snakeToCamelCase(value);
          }
          if (isPlainObject(camelCaseObject)) {        
            camelCaseObject[camelCase(key)] = value;
          }else if(isArray(camelCaseObject)){
            camelCaseObject.push(value);
          }
        }
      )
      return camelCaseObject;  
    }
    
    0 讨论(0)
  • 2020-12-04 21:32

    This is my take; more readable and with less nesting than brandoncode's implementation, and with more room for handling edge cases like Date (which isn't handled, by the way) or null:

    function convertPropertiesToCamelCase(instance) {
        if (instance instanceof Array) {
            var result = [];
    
            for (var i = 0; i < instance.length; i++) {
                result[i] = convertPropertiesToCamelCase(instance[i]);
            }
    
            return result;
        }
    
        if (typeof instance != 'object') {
            return instance;
        }
    
        var result = {};
    
        for (var key in instance) {
            if (!instance.hasOwnProperty(key)) {
                continue;
            }
    
            result[key.charAt(0).toLowerCase() + key.substring(1)] = convertPropertiesToCamelCase(instance[key]);
        }
    
        return result;
    }
    
    0 讨论(0)
  • 2020-12-04 21:35

    Here's handy library you might wanna try: https://www.npmjs.com/package/camelize2

    You simply need to install it with npm install --save camelize2 and then

    const camelize = require('camelize2')
    
    const response = {
       Contacts: [{ GivenName: "Matt", FamilyName:"Berry" }]
    }
    
    const camelizedResponse = camelize(response)
    
    0 讨论(0)
提交回复
热议问题