user1024941 user1024941 - 2 months ago 5
Javascript Question

Convert string to json with multiple objects

I am using angular formly to generate some forms. I'm retrieving the form definition from my database, which is multiple json objects. In javascript how to I convert (for example) the following string into an array of objects?

"{"template": "<h2>MAIN HEADING.</h2>"
"template": "<h4>sub heading.</h4>"


Just wrap it in [] so it becomes a JSON array, and then parse it:

var result = JSON.parse( '[' + str + ']' );