Search code examples
javascriptregexjsonstringsingle-quotes

Parsing/manipulating weirdly formated json array


I have the following broken javascript json string

[ 'some string's', 'inside, array', 60.4, 10.3, '' ]

I would like to parse this broken json string, scraped from the script part of a web page, into a javascript array - but as you see it's not valid json. The strings are single quoted and can also have a single quotes inside the string. So i need to replace the single quotes surrounding the string items with double quotes, without changing the single quotes inside the string.

Any regex magicians out here?

Update: The strings could also contain commas, and the items can also be numbers.

Tip: Look for the pattern ', ' to separated string items might work.


Solution

  • Assuming your "bad" JSON strings always start with [ ' and end with ' ], we can first "trim" the string, and then split using ', '.

        var str = "[ 'some string's', 76, 'inside, array', '', 60.4, 10.3, '', 56 ]";
        str = str.replace(/^\[ | \]$/g, '')
        var re = /'(.*?)'(?=,|$)|(\d+(?:\.\d+)?)(?=,|$)/g; 
        var m;
        arr = [];
        
        while ((m = re.exec(str)) !== null) {
            if (m.index === re.lastIndex) {
                re.lastIndex++;
            }
            if (m[1] != undefined) {
               arr.push(m[1]);
               document.getElementById("res").innerHTML += m[1] + "<br/>";
            }
            else
            {
               arr.push(m[2]);
               document.getElementById("res").innerHTML += m[2] + "<br/>";
            }
        }
        alert(arr);
    <div id="res"/>

    However, mind that the best way to fix JSON is to fix it on the JSON provider's side. If you have more complicated JSON strings, this solution will need enhancing.