Fastest way to flatten / un-flatten nested JSON objects

I threw some code together to flatten and un-flatten complex/nested JSON objects. It works, but it’s a bit slow (triggers the ‘long script’ warning).

For the flattened names I want “.” as the delimiter and [INDEX] for arrays.

Examples:

un-flattened | flattened
---------------------------
{foo:{bar:false}} => {"foo.bar":false}
{a:[{b:["c","d"]}]} => {"a[0].b[0]":"c","a[0].b[1]":"d"}
[1,[2,[3,4],5],6] => {"[0]":1,"[1].[0]":2,"[1].[1].[0]":3,"[1].[1].[1]":4,"[1].[2]":5,"[2]":6}

I created a benchmark that ~simulates my use case http://jsfiddle.net/WSzec/

  • Get a nested JSON object
  • Flatten it
  • Look through it and possibly modify it while flattened
  • Unflatten it back to it’s original nested format to be shipped away

I would like faster code: For clarification, code that completes the JSFiddle benchmark (http://jsfiddle.net/WSzec/) significantly faster (~20%+ would be nice) in IE 9+, FF 24+, and Chrome 29+.

Here’s the relevant JavaScript code: Current Fastest: http://jsfiddle.net/WSzec/6/

JSON.unflatten = function(data) {
    "use strict";
    if (Object(data) !== data || Array.isArray(data))
        return data;
    var result = {}, cur, prop, idx, last, temp;
    for(var p in data) {
        cur = result, prop = "", last = 0;
        do {
            idx = p.indexOf(".", last);
            temp = p.substring(last, idx !== -1 ? idx : undefined);
            cur = cur[prop] || (cur[prop] = (!isNaN(parseInt(temp)) ? [] : {}));
            prop = temp;
            last = idx + 1;
        } while(idx >= 0);
        cur[prop] = data[p];
    }
    return result[""];
}
JSON.flatten = function(data) {
    var result = {};
    function recurse (cur, prop) {
        if (Object(cur) !== cur) {
            result[prop] = cur;
        } else if (Array.isArray(cur)) {
             for(var i=0, l=cur.length; i<l; i++)
                 recurse(cur[i], prop ? prop+"."+i : ""+i);
            if (l == 0)
                result[prop] = [];
        } else {
            var isEmpty = true;
            for (var p in cur) {
                isEmpty = false;
                recurse(cur[p], prop ? prop+"."+p : p);
            }
            if (isEmpty)
                result[prop] = {};
        }
    }
    recurse(data, "");
    return result;
}

EDIT 1 Modified the above to @Bergi ‘s implementation which is currently the fastest. As an aside, using “.indexOf” instead of “regex.exec” is around 20% faster in FF but 20% slower in Chrome; so I’ll stick with the regex since it’s simpler (here’s my attempt at using indexOf to replace the regex http://jsfiddle.net/WSzec/2/).

EDIT 2 Building on @Bergi ‘s idea I managed to created a faster non-regex version (3x faster in FF and ~10% faster in Chrome). http://jsfiddle.net/WSzec/6/ In the this (the current) implementation the rules for key names are simply, keys cannot start with an integer or contain a period.

Example:

  • {“foo”:{“bar”:[0]}} => {“foo.bar.0”:0}

EDIT 3 Adding @AaditMShah ‘s inline path parsing approach (rather than String.split) helped to improve the unflatten performance. I’m very happy with the overall performance improvement reached.

The latest jsfiddle and jsperf:

http://jsfiddle.net/WSzec/14/

http://jsperf.com/flatten-un-flatten/4

17 Answers
17

Leave a Comment