Вот решение в coffeescript, использующее библиотеку подчеркивания. Если вы не используете это, вы можете заменить _.foldl на цикл for.
parseNonStrictJson = (value) ->
inQuote = false
correctQuotes = (memo, nextChar) ->
insertQuote =
(inQuote and not /[a-z0-9_"]/.test nextChar) or
(!inQuote and /[a-z_]/.test nextChar)
inQuote = (inQuote != (insertQuote or nextChar == '"') )
memo + (if insertQuote then '"' else '') + nextChar
valueWithQuotes = _.foldl(value + '\n', correctQuotes, "")
JSON.parse(valueWithQuotes)
И то же самое в javascript:
function parseNonStrictJson(value) {
var correctQuotes, inQuote, valueWithQuotes;
inQuote = false;
correctQuotes = function(memo, nextChar) {
var insertQuote;
insertQuote = (inQuote && !/[a-z0-9_"]/.test(nextChar)) || (!inQuote && /[a-z_]/.test(nextChar));
inQuote = inQuote !== (insertQuote || nextChar === '"');
return memo + (insertQuote ? '"' : '') + nextChar;
};
valueWithQuotes = _.foldl(value + '\n', correctQuotes, "");
return JSON.parse(valueWithQuotes);
};