Rectangle 27 0

Efficiently renamere map javascriptjson object keys within structured array?

1389822740739: Beginning regex rename test
1389822740761: Regex rename complete
// 22ms, 22ms, 21ms
1389822740762: Beginning parse and remap in for loop test
1389822740831: For loop remap complete
// 69ms, 68ms, 68ms
1389822740831: Beginning reviver function test
1389822740893: Reviver function complete
// 62ms, 61ms, 60ms
fs = require('fs');
fs.readFile('test.json', 'utf8', function (err, data) {
    if (err) {
        return console.log(err);
    console.log(new Date().getTime() + ": Beginning regex rename test");
    var str = data.replace(/"title":/g, '"name":');
    str = str.replace(/"uid":/g, '"id":');
    console.log(new Date().getTime() + ": Regex rename complete");
    console.log(new Date().getTime() + ": Beginning parse and remap in for loop test");
    var arr = JSON.parse(data);
    for (var i = 0; i < arr.length; i++) {
        arr[i].name = arr[i].title;
        arr[i].id = arr[i].uid;
        delete arr[i].title;
        delete arr[i].uid;
    console.log(new Date().getTime() + ": For loop remap complete");
    console.log(new Date().getTime() + ": Beginning reviver function test");
    var arr = JSON.parse(data, function (prop, value) {
        switch (prop) {
            case "title":
       = value;
            case "uid":
       = value;
                return value;
    console.log(new Date().getTime() + ": Reviver function complete");
str = str.replace(/"title":/g, '"name":');
var arr = JSON.parse(str, function(prop, value) {
   switch(prop) {
     case "title": = value;
     case "uid": = value;
        return value;

@remus: Yep, that would be cool! Sorry, I don't have the time now to make those tests myself.

As I already mentioned in the comments, if you can make certain assumptions about the values of the objects, you could use a regular expression to replace the keys, for example:

Awesome! Nice to see that the reviver function is at least a little faster (though not by much). And yeah, you have to be careful with regular expressions and be certain about the input you get.

Benchmarks, using the Node.js script below to test 3 times:

If you have to parse the JSON anyway, a more structured approach would be to pass a reviver function to JSON.parse and you might be able to avoid an additional pass over the array. This probably depends on how engine implement JSON.parse though (maybe they parse the whole string first and then make a second pass with the reviver function, in which case you wouldn't get any advantage).

It's not as "clean", but it might get the job done faster.

Luckily in my case I'm using consistent data with a consistent schema, so it won't be a problem. And also in my case, since I need this to server thousands of requests in Node, I want it to be as efficient as possible. Thanks!

OK, updated. Hope you don't mind I hijacked it like that, but there's a wealth of information there now.

Still looks like the regex is the quickest -- I'm only testing this in a simple Node.js instance, I can update your answer with the benchmarks if you like?

Test script, loading 100,230 lines of the OP's sample JSON: