简体   繁体   中英

How to remove double quote in broken nested json values?

Am stuck with applying replace() with regex for broken nested json value. For example, i want to remove doublequote for origin value below.

How to change from this:

{
 "type" : "fruitList",
 "data" : [{
   "fruitName" : "test",
   "origin" : "[{"states" : "USA"}]"
 }]
}

To this:

{
 "type" : "fruitList",
 "data" : [{
   "fruitName" : "test",
   "origin" : [{"states" : "USA"}]
 }]
}

You could simply use 2 replace without RegEx:

jsonstring.replace('"[', "[").replace(']"', "]")

Or if you just want to use a single replace with RegEx:

jsonstring.replace(/("\[|\]")/g, match => {
   return match === '"[' ? "[" : "]"
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM