How can I save data in JSON format which looks like this: 
    [
    {"modules1":
        {"noofsubmodules":"2",
         "cmheading":"Chapter 1",
         "submodules":["lesson 1", "lesson 2"],
        }
    },
    {"modules2":
        {
            "noofsubmodules":"1",
            "cmheading":"Module Title",
            "submodules": ["Submodule 1"]
        }
    },
    {"modules3":
        {
            "noofsubmodules":"3",
            "cmheading":"Module Title",
            "submodules": ["L1","L2","L3"]
        }
    },
...
...
    ]
Other than using this : 
for (var key in p) {
    if (p.hasOwnProperty(key)) {
        console.log(key + " -> " + p[key]);
    }
}
I don't know the number of modules, It depends on client only.
and following Answers on this question and other questions as well didn't help: How do I loop through or enumerate a JavaScript object?
