Received a JSON from the backend to the front end that uses single quotes throughout, causing issues with a Magento 2 widget. The JSON structure is as follows:
{ 'mood': 'happy', 'reason': 'why shouldn't I?'}
The task at hand is to convert all single quotes to double quotes without altering the apostrophes within. Using a regular expression seems to be the most efficient approach by targeting JSON-related single quotes for replacement.
Despite various attempts and solutions found on SO, none have been successful in the past hour and a half. The closest solution attempted was:
goodJson = brokenJson.replace(/(?<=[{,:] )'|'(?=[:,]| })/g, '"');
This regex pattern aims to replace single quotes preceded or followed by specific characters, but it falls short when the last quote next to a closing curly brace remains unaffected in the output.
If you can shed some light on why this method fails and suggest an improved approach to achieve the desired result, it would be greatly appreciated.