json - Replace quotes by double quotes in javascript -


i search regex or other way replace quotes double quotes. keep quote in word. example keep quote in jusqu'à. try find regular expression didn't find.

input:

{'nom': 'mulot', 'code': 'apodemus agrarius', 'descriptif': 'mammifère', 4'reconnaissance': "petit mammifère rongeur sauteur jusqu'à 80 cm et nageur.", 'conditions': ' ', 'sources': ' '} 

output:

{"nom": "mulot", "code": "apodemus agrarius", "descriptif": "mammifère", "reconnaissance": "petit mammifère rongeur sauteur jusqu'à 80 cm et nageur.", "conditions": ' ', "sources": " "} 

thanks lot.

well need split on commas, loop on that, split on colons, loop on that, replace quotes, join together, , join first split together.

var str = "{'nom': 'mulot', 'code': 'apodemus agrarius', 'descriptif': 'mammifère', 'reconnaissance': \"petit mammifère rongeur sauteur jusqu'à 80 cm et nageur.\", 'conditions': ' ', 'sources': ' '}";    var rep = "{" + str.substr(1, str.length - 2).split(/\s?,\s?/).map(function(v) {    return v.split(/\s?:\s?/).map(function(p) {      return p.replace(/^\s?'([^']+)'\s?$/, '"$1"');    }).join(":");  }).join(",") + "}";      console.log(rep);

problems this"

  1. it not check " inside of string. cause issue.
  2. if string contains , break
  3. if string contains : break
  4. does not handle arrays/objects inside

another solution (which not 100% safe) eval() or new function()

var str = "{'nom': 'mulot', 'code': 'apodemus agrarius', 'descriptif': 'mammifère','reconnaissance': \"petit mammifère rongeur sauteur jusqu'à 80 cm et nageur.\", 'conditions': ' ', 'sources': ' '}";    var arr = new function("return " + str)();  console.log(arr);  console.log(json.stringify(arr));

now best solution, fix whatever outputting/inputting in first place.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -