Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON.stringify, change the case of the key

Tags:

I'm consuming a web service that returns json, and storing the json in a local variable. The json represents a simple business object such as:

var entry = {   "FirstName": "John",   "LastName": "Doe",   .... }; 

The casing is like that because it matches up with the property names from the .net class, as per our naming convention.

When a change a few of these properties and pass back the json, the web service now expects camel case (again, as per our naming convention for method parameters) instead of the pascal case initially returned.

var entry = {   "firstName": "John",   "lastName": "Doe",   .... }; 

This of course doesn't work.

I'm using JSON.stringify to send the json back to the web service as a string, and I was looking to see if there was an easy way to change the key to camel case. However, it looks like I can only use the replacer param to work with the value.

I could change the serialization of the class, but lets pretend that's not an option. Any ideas?

Thanks.

like image 546
ScottE Avatar asked Mar 30 '11 00:03

ScottE


People also ask

Does JSON Stringify change the object?

The JSON.stringify() method converts a JavaScript object or value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.

Is it bad to use JSON Stringify?

It`s ok to use it with some primitives like Numbers, Strings or Booleans. As you can see, you can just lose unsupported some data when copying your object in such a way. Moreover, JavaScript won`t even warn you about that, because calling JSON. stringify() with such data types does not throw any error.

What is the purpose of JSON Stringify () and parse () methods?

The JSON. parse() function is used to convert a string into a JavaScript object while the JSON. stringify() function is used to convert a JavaScript object into a string.

Does JSON Stringify escape quotes?

JSON. stringify does not act like an "identity" function when called on data that has already been converted to JSON. By design, it will escape quote marks, backslashes, etc. You need to call JSON.


1 Answers

You can use a JSON replacer to switch keys before writing.

JSON.stringify(myVal, function (key, value) {   if (value && typeof value === 'object') {     var replacement = {};     for (var k in value) {       if (Object.hasOwnProperty.call(value, k)) {         replacement[k && k.charAt(0).toLowerCase() + k.substring(1)] = value[k];       }     }     return replacement;   }   return value; }); 

For the opposite, you can use a JSON reviver.

JSON.parse(text, function (key, value) {     if (value && typeof value === 'object')       for (var k in value) {         if (/^[A-Z]/.test(k) && Object.hasOwnProperty.call(value, k)) {           value[k.charAt(0).toLowerCase() + k.substring(1)] = value[k];           delete value[k];         }       }       return value;     }); 

The second optional argument is a function that is called with every value created as part of the parsing or every value about to be written. These implementations simply iterate over keys and lower-cases the first letter of any that have an upper-case letter.

There is documentation for replacers and revivers at http://json.org/js.html :

The optional reviver parameter is a function that will be called for every key and value at every level of the final result. Each value will be replaced by the result of the reviver function. This can be used to reform generic objects into instances of pseudoclasses, or to transform date strings into Date objects.

The stringifier method can take an optional replacer function. It will be called after the toJSON method (if there is one) on each of the values in the structure. It will be passed each key and value as parameters, and this will be bound to object holding the key. The value returned will be stringified.

like image 95
Mike Samuel Avatar answered Oct 20 '22 14:10

Mike Samuel