Myat Thu
Myat Thu

Reputation: 3

can't extract array of objects in JSON

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

Upvotes: 0

Views: 132

Answers (1)

Alan Friedman
Alan Friedman

Reputation: 1610

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)

Upvotes: 1

Related Questions