3

I have a JSON object with 3 nested objects at first level.

data{"key1":"value1", "key2":"value2",
    "section1":"{"key1_1":"value1_1", "key1_2":"value1_2"}",
    "section2":"{"key2_1":"value2_1", "key2_2":"value2_2", "key2_3":"value2_3"}",
    "section3":"{"key3_1":"value3_1"}"
}

Now, using NODE.JS, I need to move all key-value from nested to parent object with all keys-values at root level, like this:

data{
"key1":"value1", "key2":"value2","key1_1":"value1_1", "key1_2":"value1_2", "key2_1":"value2_1", "key2_2":"value2_2", "key2_3":"value2_3", "key3_1":"value3_1"
}
6
  • 1
    What you have posted, is neither valid JSON, or even valid Javascript Object.. Commented Nov 4, 2016 at 14:42
  • unfortunately data is exactly like that... with :"{". These are JSON strings inside sections. Commented Nov 4, 2016 at 16:28
  • how do you know, which one contains serialized data? Commented Nov 5, 2016 at 8:50
  • source owner assured me Commented Nov 5, 2016 at 10:07
  • @HassanNaqvi, i do not understand which property does contain a json string? Commented Nov 5, 2016 at 11:09

2 Answers 2

6

With a proper object, you could iterate the keys and if an object is found, call the function again with the content of the found object.

function flat(source, target) {
    Object.keys(source).forEach(function (k) {
        if (source[k]!== null && typeof source[k] === 'object') {
            flat(source[k], target);
            return;
        }
        target[k] = source[k];
    });
}

var data = { "key1": "value1", "key2": "value2", "section1": { "key1_1": "value1_1", "key1_2": "value1_2" }, "section2": { "key2_1": "value2_1", "key2_2": "value2_2", "key2_3": "value2_3" }, "section3": { "key3_1": "value3_1" } },
    flatObject = {};

flat(data, flatObject);
console.log(flatObject);

Version with JSON strings as value and a try ... catch statement for testing if the value is a JSON.

function flat(source, target) {
    Object.keys(source).forEach(function (k) {
        var o;
        try {
            o = JSON.parse(source[k]);
            if (o && typeof o === 'object') {
                flat(o, target);
            } else {
                target[k] = o;
            }
        } catch (e) {
            target[k] = source[k];
        }
    });
}

var data = { "key1": "value1", "key2": "value2", "section1": "{\"key1_1\":\"value1_1\", \"key1_2\":\"value1_2\"}", "section2": "{\"key2_1\":\"value2_1\", \"key2_2\":\"value2_2\", \"key2_3\":\"value2_3\"}", "section3": "{\"key3_1\":\"value3_1\"}" },
    flatObject = {};

flat(data, flatObject);
console.log(flatObject);

Sign up to request clarification or add additional context in comments.

Comments

2

You can recursively traverse your data and add it to an empty map:

var data = {
    "key1":"value1", "key2":"value2",
    "section1":{"key1_1":"value1_1", "key1_2":"value1_2"},
    "section2":{"key2_1":"value2_1", "key2_2":"value2_2", "key2_3":"value2_3"},
    "section3":{"key3_1":"value3_1"}
}
var fill = function(carry, obj) {
    for (var key in obj) {
        if (!obj.hasOwnProperty(key)) {
            continue;
        }
        if (obj[key] instanceof Object) {
            fill(carry, obj[key]);
        }
        else {
            carry[key] = obj[key];
        }
    }
    return carry;
}
var output = {}
fill(output, data);
console.log(output);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.