0

can't extract data from JSON array object data i have this json wrong format data

    "ratings":"[{'id': 2, 'name': 'Confusing', 'count': 4}, {'id': 11, 
    'name': 'Longwinded', 'count': 8}, {'id': 26, 'name': 'Obnoxious', 
    'count': 1}, {'id': 8, 'name': 'Informative', 'count': 236}, {'id': 10, 
    'name': 'Inspiring', 'count': 123}, {'id': 22, 'name': 'Fascinating', 
    'count': 104}, {'id': 24, 'name': 'Persuasive', 'count': 73}, {'id': 1, 
    'name': 'Beautiful', 'count': 40}, {'id': 3, 'name': 'Courageous', 
    'count': 16}, {'id': 7, 'name': 'Funny', 'count': 18}, {'id': 25, 'name': 
    'OK', 'count': 29}, {'id': 9, 'name': 'Ingenious', 'count': 13}, {'id': 
    21, 'name': 'Unconvincing', 'count': 4}, {'id': 23, 'name': 'Jaw- 
    dropping', 'count': 8}]"

so i replace the double quote with this.

   var newJson = obj.replace(/([a-zA-Z0-9]+?):/g, '"$1":');
   newJson = newJson.replace(/'/g, '"');

and the result is this.

    "ratings":"[{"id": 21, "name": "Unconvincing", "count": 21}, {"id": 8, 
    "name": "Informative", "count": 25}, {"id": 10, "name": "Inspiring", 
    "count": 26}, {"id": 25, "name": "OK", "count": 11}, {"id": 22, "name": 
    "Fascinating", "count": 9}, {"id": 9, "name": "Ingenious", "count": 14}, 
    {"id": 2, "name": "Confusing", "count": 3}, {"id": 26, "name": 
    "Obnoxious", "count": 4}, {"id": 1, "name": "Beautiful", "count": 12}, 
    {"id": 11, "name": "Longwinded", "count": 9}, {"id": 24, "name": 
    "Persuasive", "count": 5}, {"id": 23, "name": "Jaw-dropping", "count": 
    3}, {"id": 3, "name": "Courageous", "count": 0}, {"id": 7, "name": 
    "Funny", "count": 0}]"

so i want to remove two double quotes not all double quotes. i want the data format look like this.

    "ratings":[{"id": 21, "name": "Unconvincing", "count": 21}, {"id": 8, 
    "name": "Informative", "count": 25}, {"id": 10, "name": "Inspiring", 
    "count": 26}, {"id": 25, "name": "OK", "count": 11}, {"id": 22, "name": 
    "Fascinating", "count": 9}, {"id": 9, "name": "Ingenious", "count": 14}, 
    {"id": 2, "name": "Confusing", "count": 3}, {"id": 26, "name": 
    "Obnoxious", "count": 4}, {"id": 1, "name": "Beautiful", "count": 12}, 
    {"id": 11, "name": "Longwinded", "count": 9}, {"id": 24, "name": 
    "Persuasive", "count": 5}, {"id": 23, "name": "Jaw-dropping", "count": 
    3}, {"id": 3, "name": "Courageous", "count": 0}, {"id": 7, "name": 
    "Funny", "count": 0}] 

Can someone pls tell me how to remove those two double quotes. i try with many regex and can't get still desired result
Myat Thu
  • 3
  • 3
  • Instead of messing around with multiple `.replace()` calls you should fix whoever produces the invalid JSON – Andreas Jul 11 '19 at 11:52
  • JSON dataset is an online dataset & record is just 25550 records .i can't remove this manually. That's why & than you for your advice @Andreas – Myat Thu Jul 11 '19 at 11:55
  • What you need is `const ratings = JSON.parse(data.ratings.replace(/'/g, '"'));` (live code example: https://jsfiddle.net/khrismuc/y03bd5ok/) –  Jul 11 '19 at 11:55
  • [I once took the polyfill for `JSON.parse` and replaced the meaning of a string to consist of '' single quote notation](https://stackoverflow.com/a/51397936/3233388) – Adelin Jul 11 '19 at 11:55
  • i already remove single quote notation in JSON dataset with that you mention. but what i wann want is to delete two extra double quote in the dataset @ChrisG – Myat Thu Jul 11 '19 at 12:00
  • Did you look at my code? Those double-quotes are string delimiters, and if you stop looking at this as text and start looking at it as an object, what you have is a string: `[ ... ]`. This string is JSON except for using the wrong quotes. Which is easily fixed by replacing them. Just do `data.ratings = JSON.parse(data.ratings.replace(/'/g, '"'));` –  Jul 11 '19 at 12:02

1 Answers1

1

You can wrap the input json in braces and perform the replace on the ratings key.

 const json = {"ratings":"[{'id': 2, 'name': 'Confusing', 'count': 4}, {'id': 11, 'name': 'Longwinded', 'count': 8}, {'id': 26, 'name':'Obnoxious', 'count': 1}, {'id': 8, 'name': 'Informative', 'count': 236}, {'id': 10, 'name': 'Inspiring', 'count': 123}, {'id': 22, 'name': 'Fascinating', 'count': 104}, {'id': 24, 'name': 'Persuasive', 'count': 73}, {'id': 1, 'name': 'Beautiful', 'count': 40}, {'id': 3, 'name': 'Courageous', 'count': 16}, {'id': 7, 'name': 'Funny', 'count': 18}, {'id': 25, 'name': 'OK', 'count': 29}, {'id': 9, 'name': 'Ingenious', 'count': 13}, {'id': 21, 'name': 'Unconvincing', 'count': 4}, {'id': 23, 'name': 'Jaw- dropping', 'count': 8}]"}

 const newJSON = {ratings: json.ratings.replace(/'/g, '"')}

 const ratings = {ratings: JSON.parse(newJSON.ratings)}

 console.log(ratings)
Alan Friedman
  • 1,582
  • 9
  • 7