user1024941 user1024941 - 1 year ago 101
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>"

Answer Source

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

var result = JSON.parse( '[' + str + ']' );
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download